标签:add ring seajs 参数 页面 创建 rem 全局 set
|
1
2
3
|
exports.hello = function() { console.log(‘Hello hangge.com‘);} |
|
1
2
|
var hangge = require(‘./hangge‘);hangge.hello(); |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//私有变量var test = 110;//公开方法function Hangge() { var name; this.setName = function(thyName) { name = thyName; }; this.hello = function() { console.log(‘Hello ‘ + name); };};module.exports = Hangge; |
|
1
2
3
4
|
var Hangge = require(‘./hangge‘);var hello = new Hangge();hello.setName(‘hangge.com‘);hello.hello(); |
|
1
2
3
4
5
6
7
8
9
|
//圆面积计算export function area(radius) { return Math.PI * radius * radius;}//圆周长计算export function circumference(radius) { return 2 * Math.PI * radius;} |
(2)创建一个 main.js 文件,引入这个模块并调用。这里 import 命令使用大括号的形式加载模块对外的接口。
|
1
2
3
|
import {area,circumference} from ‘./hangge‘;console.log(‘圆面积:‘ + area(10));console.log(‘圆周长:‘ + circumference(11)); |
当然也可以使用星号(*)指定一个对象,实现模块的整体加载。
|
1
2
3
|
import * as circle from ‘./hangge‘;console.log(‘圆面积:‘ + circle.area(10));console.log(‘圆周长:‘ + circle.circumference(11)); |
(3)由于 NodeJS 目前还不支持 ES2015 的 Module,这里我们借助 babel-node 来执行,运行结果如下:
|
1
2
3
4
5
6
7
8
9
|
//圆面积计算(作为默认接口)export default function(radius) { return Math.PI * radius * radius;}//圆周长计算export function circumference(radius) { return 2 * Math.PI * radius;} |
|
1
2
3
|
import area, {circumference} from ‘./hangge‘;console.log(‘圆面积:‘ + area(10));console.log(‘圆周长:‘ + circumference(11)); |
|
1
2
3
4
5
6
7
8
|
define(function (require, exports, module) { var reqModule = require("./someModule"); requModule.test(); exports.asplode = function () { //someing }}); |
|
1
2
3
4
5
6
7
8
|
define(function(){ var add = function(x,y) { return x + y; }; return { add : add }}); |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!DOCTYPE html><html> <head> <script type="text/javascript" src="require.js"></script> <script type="text/javascript"> require([‘hangge‘], function (m){ console.log(m.add(2,3)); }); </script> </head> <body> </body></html> |
|
1
2
3
4
5
6
7
8
9
10
11
12
|
define(["./cart", "./inventory"], function(cart, inventory) { //return an object to define the "my/shirt" module. return { color: "blue", size: "large", addToCart: function() { inventory.decrement(this); cart.add(this); } } }); |
|
1
2
3
4
5
6
7
8
|
define(function(require, exports) { // 对外提供name属性 exports.name = ‘hangge‘; // 对外提供hello方法 exports.hello = function() { console.log(‘Hello hangge.com‘); };}); |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!DOCTYPE html><html> <head> <script type="text/javascript" src="sea.js"></script> <script type="text/javascript"> //加载一个模块,在加载完成时,执行回调 seajs.use(‘hangge‘, function(a) { a.hello(); }); </script> </head> <body> </body></html> |
|
1
2
3
4
5
6
7
8
9
|
define(function(require, exports, module) { // 对外提供接口 module.exports = { name: ‘hangge‘, hello: function() { console.log(‘Hello hangge.com‘); } };}); |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!DOCTYPE html><html> <head> <script type="text/javascript" src="sea.js"></script> <script type="text/javascript"> //加载一个模块,在加载完成时,执行回调 seajs.use(‘hangge‘, function(a) { a.hello(); }); </script> </head> <body> </body></html> |
原文出自:www.hangge.com 转载请保留原文链接:https://www.hangge.com/blog/cache/detail_1686.html
JS - CommonJS、ES2015、AMD、CMD模块规范对比与介绍(附样例)
标签:add ring seajs 参数 页面 创建 rem 全局 set
原文地址:https://www.cnblogs.com/jiaqi1719/p/12565392.html