Common JS vs.AMD

1840 ワード

Javascriptでmoduleを定義する方式は二つあります.Common JS仕様、AMD仕様.この2つの語のより全面的な定義を並べて、視聴しています.
Common JS
CommunJS is a project to define a common API and ecosystem for JavaScript.One part of Common JS is the Module specification.Node.js and RingoJS are server-side JavaScript runtimes,and yes,both the module modem
// mylib.js
// package/lib is a dependency we require
var lib = require( "package/lib" );

// behavior for our module
function foo(){
    lib.log( "hello world!" );
}

// export (expose) foo to other modules as foobar
exports.foobar = foo;
使用
//  main.js ( mylib.js     )
var my = require('./mylib');
my.foobar();
AMD
AMD(AAyncchronous Module Definition)isanothe speciimiation for modules.RequireJS is probably the most poppurimplemenation of AMD.One majorr difference from CommmmmmmmmmuJS isisaaaaaaatespeciiiiifes specaatstsatsaaaaatsaaateteteteteteteaaaaaaaaaatsaaaaaaaaaaaaaaaaaaaaaaaaatsaaaaaaaaaaaaaaaaaaaaaaaaaby waiting for a load to finish.
AMD is generary more used in client-side JavaScript development due to this、and Common JS Modules are server-side.
It is unrelated to the technology company AMD and the processors it makes.
// mylib.js
// package/lib is a dependency we require
define(["package/lib"], function (lib) {

    // behavior for our module
    function foo() {
        lib.log( "hello world!" );
    }

    // export (expose) foo to other modules as foobar
    return {
        foobar: foo
    }
});
使用
同期方式(同上)
//  main.js ( mylib.js     )
var my = require('./mylib');
my.foobar();
非同期モード
//  main.js ( mylib.js     )
require(['./mylib'], function(my) {
    my.foobar();
});
参照
Thanks to stackover flow