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
例
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.
例
同期方式(同上)
Thanks to stackover flow
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();
AMDAMD(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