CommonJS是NodeJS在推廣過程中對子產品定義的規範化産出。
RequireJS、SeaJS都基于CommonJS規範來的.
AMD是RequireJS在推廣過程中對子產品定義的規範化産出。
CMD是SeaJS在推廣過程中對子產品定義的規範化産出。
(module.exports預設整個檔案為一個子產品,前端不适用,就出現了define定義子產品(名))
CommonJS規範
-
引用require,子產品定義輸出exports.sum = function(){} ,子產品辨別module.exports
require引用的實質是:module.exports
exports與module.exports的差別這邊不做說明,請檢視:module.exports和exports的差別
Node入門示例:
建立math.js,代碼如下:
module.exports = {
add(a,b){
return a+b
}
}
建立index.js,在index.js引入math.js
let math = require(./math);
console.log(math.add(1,5));
在cmd指令行運作:node index
AMD規範
定義子產品:define(‘子產品名’,[],function(){
});
引用 require