詳解Sea.js中Module.exports和exports的區(qū)別
一、官方解釋
因為SeaJs和Nodejs都是基于CommonJS,所以直接看的Node的官方文檔解釋 Module.exports The module.exports object is created by the Module system. Sometimes this is not acceptable; many want their module to be an instance of some class. To do this, assign the desired export object to module.exports. Note that assigning the desired object to exports will simply rebind the local exports variable, which is probably not what you want to do. module.exports對象是由模塊系統(tǒng)創(chuàng)建的。 有時這是難以接受的;許多人希望他們的模塊成為某個類的實例。 為了實現(xiàn)這個,需要將期望導出的對象賦值給module.exports。 注意,將期望的對象賦值給exports會簡單地重新綁定到本地exports變量上,這可能不是你想要的。
exports
The exports variable is available within a module's file-level scope, and is assigned the value of module.exports before the module is evaluated. It allows a shortcut, so that module.exports.f = ... can be written more succinctly as exports.f = .... However, be aware that like any variable, if a new value is assigned to exports, it is no longer bound to module.exports: 譯文:exports變量是在模塊的文件級別作用域內(nèi)有效的,它在模塊被執(zhí)行前被賦于 module.exports 的值。它有一個快捷方式,以便 module.exports.f = ... 可以被更簡潔地寫成exports.f = ...。 注意,就像任何變量,如果一個新的值被賦值給exports,它就不再綁定到module.exports(其實是exports.屬性會自動掛載到?jīng)]有命名沖突的module.exports.屬性)
require
從require導入方式去理解,關鍵有兩個變量(全局變量module.exports
,局部變量exports
)、一個返回值(module.exports
)
function require(...) { var module = { exports: {} }; ((module, exports) => { // 你的被引入代碼 Start // var exports = module.exports = {}; (默認都有的) function some_func() {}; exports = some_func; // 此時,exports不再掛載到module.exports, // export將導出{}默認對象 module.exports = some_func; // 此時,這個模塊將導出some_func對象,覆蓋exports上的some_func // 你的被引入代碼 End })(module, module.exports); // 不管是exports還是module.exports,最后返回的還是module.exports return module.exports; }
二、Demo事例
事例一:1.js
console.log(exports); // {} console.log(module.exports); // {} console.log(exports === module.exports); // true console.log(exports == module.exports); // true /** Module { id: '.', exports: {}, parent: null, filename: '/1.js', loaded: false, children: [], paths: [ '/node_modules' ] } */ console.log(module);
從事例一中,可以看出來
1.每個js文件一創(chuàng)建,都有一個var exports = module.exports = {};
,使exports
和module.exports
都指向一個空對象。
2.module
是全局內(nèi)置對象,exports
是被var
創(chuàng)建的局部對象。
3.module.exports
和exports
所指向的內(nèi)存地址相同
事例二:2.js、3.js
// 2.js exports.id = 'exports的id'; exports.id2 = 'exports的id2'; exports.func = function(){ console.log('exports的函數(shù)'); }; exports.func2 = function() { console.log('exports的函數(shù)2'); }; module.exports = { id: 'module.exports的id', func:function(){ console.log('module.exports的函數(shù)'); } };
// 3.js var a = require('./2.js'); // 當屬性和函數(shù)在module.exports都有定義時: console.log(a.id); // module.exports的id console.log(a.func()); // module.exports的函數(shù) // 當屬性在module.exports沒有定義,函數(shù)在module.exports有定義 console.log(a.id2); // undefined console.log(a.func()); // module.exports的函數(shù) // 當函數(shù)在module.exports沒有定義,屬性在module.exports有定義 console.log(a.id); // module.exports的id console.log(a.func2()); // 報錯了 TypeError: a.func2 is not a function
由例二可以知道:
1.module.exports
像是exports
的大哥,當module.exports
以{}
整體導出時會覆蓋exports
的屬性和方法,
2.注意,若只是將屬性/方法掛載在module.exports./exports.
上時,exports.id=1
和module.exports.id=100
,module.exports.id=function(){}
和exports.id=function(){}
,最后id的值取決于exports.id
和module.exports.id
的順序,誰在后,就是最后的值
3.若exports
和module.exports
同時賦值時,exports
所使用的屬性和方法必須出現(xiàn)在module.exports
,若屬性沒有在module.exports
中定義的話,出現(xiàn)undefined
,若方法沒有在module.exports
中定義,會拋出TypeError
錯誤。
例三 4.js、5.js
module.exports
的對象、prototype
、構造函數(shù)使用
// 4.js var a = require('./5.js'); // 若傳的是類,new一個對象 var person = new a('Kylin',20); console.log(person.speak()); // my name is Kylin ,my age is 20 // 若不需要在構造函數(shù)時初始化參數(shù),直接調(diào)用方法/屬性 // a.speak(); // my name is kylin ,my age is 20
// 5.js // Person類 function Person(name,age){ this.name = name; this.age = age; } // 為類添加方法 Person.prototype.speak = function(){ console.log('my name is '+this.name+' ,my age is '+this.age); }; // 返回類 module.exports = Person; // 若構造函數(shù)沒有傳入?yún)?shù)(name,age),直接傳入對象 // module.exports = new Person('kylin',20);
說了這么多,其實建議就是,如果只是單一屬性或方法的話,就使用exports.
屬性/方法。要是導出多個屬性或方法或使用對象構造方法,結合prototype
等,就建議使用module.exports = {}
。文章有很多地方描述的可能不是很準確,提到的點也不夠全面,如果有不對的地方,還望斧正!
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關文章
詳解Sea.js中Module.exports和exports的區(qū)別
最近在看Seajs時,看到了exports.doSomething和module.exports,想對這兩者的區(qū)別一探究竟。所以下面這篇文章主要介紹了Sea.js中Module.exports和exports的區(qū)別,需要的朋友可以參考借鑒,一起來看看吧。2017-02-02Seajs是什么及sea.js 由來,特點以及優(yōu)勢
這篇文章主要介紹了Seajs的相關知識和和學習心得,適合剛接觸SeaJS的同學,需要的朋友可以參考下,有更好的新手教程或文檔,歡迎推薦、分享2016-10-10