1. CommonJS

关于CommonJS的介绍和使用
CommonJS遵循CMD规范
CMD很多特性面向服务器端,无法再客户端/浏览器实现——比如iosystem等。
CMD模块只能定义对象。

特点

  • 一个CJS模块是一段可复用的Javascript,它导入一些列特定的对象给依赖它的代码调用(没有define函数包裹)。
  • CJS有一个exports变量,用于向其它模块提供对象。
  • 还有一个require函数,用于引用其它依赖模块。


使用

require()exports
// package/lib 是依赖模块/文件
var lib = require('package/lib');

function foo(){
    lib.log('hello world!');
}

// 把 foo 暴露给其它模块
exports.foo = foo;
exports的使用

模块定义

//构造函数,包含一些属性或方法
function foobar(){
        this.foo = function(){
                console.log('Hello foo');
        }

        this.bar = function(){
                console.log('Hello bar');
        }
}

// 把 foobar 暴露给其它模块
exports.foobar = foobar;

模块使用

// 当前文件与模块文件在同一目录路
var foobar = require('./foobar').foobar,
    test   = new foobar();

test.bar(); // 'Hello bar'


使用AMD定义模块
define(['package/lib'], function(lib){

    function foo(){
        lib.log('hello world!');
    } 

    // 把 foo 暴露给其它模块
    return {
        foobar: foo
    };
});


使用多个依赖模块

app.js

var modA = require('./foo');
var modB = require('./bar');

exports.app = function(){
    console.log('Im an application!');
}

exports.foo = function(){
    return modA.helloWorld();
}

bar.js

exports.name = 'bar';

foo.js

require('./bar');
exports.helloWorld = function(){
    return 'Hello World!!''
}
Copyright © eamiear all right reserved,powered by Gitbook该文件修订时间: 2019-06-17 16:18:09

results matching ""

    No results matching ""