Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 70a1996

Browse files
module
1 parent 17d8cd5 commit 70a1996

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

07-模块化/01-模块化规范/02-规范化使用模块.html

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
/*
1111
* @Author: victorsun
1212
* @Date: 2017-08-24 14:34:31
13-
* @Last Modified by: victorsun
14-
* @Last Modified time: 2017-08-24 14:48:26
13+
* @Last Modified by: csxiaoyao
14+
* @Last Modified time: 2018-04-15 14:25:56
1515
*/
1616

1717
/**
@@ -27,10 +27,24 @@
2727
* 浏览器,等待时间取决于网速的快慢,可能要等很长时间,浏览器处于"假死"状态
2828
* 因此,浏览器端的模块,不能采用"同步加载"(synchronous),只能采用"异步加载"(asynchronous)
2929
*/
30-
// 假定加载一个数学模块math.js
30+
// 在模块中对外输出变量(任意对象、函数、数组等) module.exports = variable; module.exports = function(){}; module.exports = { hello: fun1, greet: fun2 };
31+
// 引入其他模块输出的对象 var foo = require('other_module');
32+
// 变量隔离使用闭包 (function () { var a = XXX; })();
33+
34+
// 例1:假定加载一个数学模块math.js
3135
var math = require('math');
3236
math.add(2,3); // 5
3337

38+
// 例2:
39+
// 模块定义文件 hello.js
40+
function greet(name) {
41+
console.log('hello,' + name + '~');
42+
}
43+
module.exports = greet;
44+
// 引用模块 main.js
45+
var greet = require('./hello');
46+
greet('csxiaoyao'); // hello,csxiaoyao~
47+
3448
/**
3549
* 【 AMD 】
3650
* AMD "Asynchronous Module Definition" 异步模块定义

0 commit comments

Comments
 (0)