<script>
// aaa.js文件中,小明定义了一个变量,名称是flag,并且为true
flag = true;
// bbb.js文件中,小红也定义了flag这个变量,为false
flag = false;
// main.js文件中,小明想通过flag进行一些判断,完成后续的事情
if (flag) {
console.log("小明是个填充");
// 结果当然是代码不能正常运行
}
script>
匿名函数的解决方案:
我们可以使用匿名函数来解决方面的重名问题
在aaa.js文件中,我们使用匿名函数
<script>
(function() {
var flag = true
})()
script>
但是我们希望在main.js文件中用到flag
但是因为flag是一个局部变量
<script>
var ModuleA = (function() {
//1.定义一个对象
var obj = {};
obj.flag = true;
//2.在对象内部添加变量和方法
obj.myFunc = function(info) {
console.log(info);
};
//3.将对象返回
return obj;
})();
if (ModuleA.flag) {
console.log("小明是个天才");
}
ModuleA.myFunc("小明长的真帅");
console.log(ModuleA);
script>
<script>
module.exports = {
flag: true,
test(a, b) {
return a + b
},
demo(a, b) {
return a + b
}
}
script>
<script>
//CommonJS模块
let {
test,
demo,
flag
} = require("moduleA");
//等同于
let _mA = require("moduleA");
let test = _mA.test;
let demo = _mA.demo;
let flag = _mA.flag;
script>
<script>
//info.js
export let name = "why";
export let age = 18
export let height = 1.88
script>
<script>
let name = "why";
let age = 18;
let height = 1.88;
export {
name,
age,
height
}
script>
<script>
export function test(content) {
console.log(content);
}
export class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
run() {
console.log(this.name + "在奔跑");
}
}
script>
<script>
function test(content) {
console.log(content);
}
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
run() {
console.log(this.name + "在奔跑");
}
}
export {
test,
Person
}
script>
<script>
//info.js
export default function() {
console.log("default function");
}
script>
<script>
import myFunc from "./info.js"
myFunc()
script>
export
指令导出了模块对外提供的接口,下面我们就可以通过import
命令来加载对应的这个模块了引入两个js文件
,而且类型需要设置为module<script src="aaa.js" type="module">script>
<script src="bbb.js" type="module">script>
<script>
import {
name,
age,
height
} from "./info.js"
console.log(name, age, height);
script>
<script>
import * as info from "./info.js"
console.log(info.name, info.age, info.height);
script>
index.html
<script src="aaa.js" type="module">script>
<script src="bbb.js" type="module">script>
<script src="mmm.js" type="module">script>
aaa.js
var name = '小明'
var age = 18
var flag = true
function sum(num1, num2) {
return num1 + num2
}
if (flag) {
console.log(sum(20, 30));
}
// 1.导出方式一:
export {
flag, sum
}
// 2.导出方式二:
export var num1 = 1000;
export var height = 1.88
// 3.导出函数/类
export function mul(num1, num2) {
return num1 * num2
}
export class Person {
run() {
console.log('在奔跑');
}
}
// 5.export default
// const address = '北京市'
// export {
// address
// }
// export const address = '北京市'
// const address = '北京市'
//
// export default address
export default function (argument) {
console.log(argument);
}
bbb.js
import {sum} from './aaa.js'
var name = '小红'
var flag = false
console.log(sum(100, 200));
mmm.js
// 1.导入的{}中定义的变量
import { flag, sum } from "./aaa.js";
if (flag) {
console.log('小明是天才, 哈哈哈');
console.log(sum(20, 30));
}
// 2.直接导入export定义的变量
import { num1, height } from "./aaa.js";
console.log(num1);
console.log(height);
// 3.导入 export的function/class
import { mul, Person } from "./aaa.js";
console.log(mul(30, 50));
const p = new Person();
p.run()
// 4.导入 export default中的内容
import addr from "./aaa.js";
addr('你好啊');
// 5.统一全部导入
// import {flag, num, num1, height, Person, mul, sum} from "./aaa.js";
import * as aaa from './aaa.js'
console.log(aaa.flag);
console.log(aaa.height);