国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

詳解Sea.js中Module.exports和exports的區別

2019-11-19 17:37:53
字體:
來源:轉載
供稿:網友

一、官方解釋

因為SeaJs和Nodejs都是基于CommonJS,所以直接看的Node的官方文檔解釋Module.exportsThe 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對象是由模塊系統創建的。 有時這是難以接受的;許多人希望他們的模塊成為某個類的實例。 為了實現這個,需要將期望導出的對象賦值給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變量是在模塊的文件級別作用域內有效的,它在模塊被執行前被賦于 module.exports 的值。它有一個快捷方式,以便 module.exports.f = ... 可以被更簡潔地寫成exports.f = ...。 注意,就像任何變量,如果一個新的值被賦值給exports,它就不再綁定到module.exports(其實是exports.屬性會自動掛載到沒有命名沖突的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文件一創建,都有一個var exports = module.exports = {}; ,使exportsmodule.exports都指向一個空對象。

      2.module是全局內置對象,exports是被var創建的局部對象。

      3.module.exportsexports所指向的內存地址相同

事例二:2.js、3.js

// 2.jsexports.id = 'exports的id'; exports.id2 = 'exports的id2'; exports.func = function(){   console.log('exports的函數');};exports.func2 = function() {   console.log('exports的函數2');};module.exports = {   id: 'module.exports的id',  func:function(){    console.log('module.exports的函數');  }};
// 3.jsvar a = require('./2.js'); // 當屬性和函數在module.exports都有定義時:console.log(a.id); // module.exports的id console.log(a.func()); // module.exports的函數// 當屬性在module.exports沒有定義,函數在module.exports有定義console.log(a.id2); // undefined console.log(a.func()); // module.exports的函數// 當函數在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=1module.exports.id=100module.exports.id=function(){}exports.id=function(){} ,最后id的值取決于exports.idmodule.exports.id的順序,誰在后,就是最后的值

      3.若exportsmodule.exports同時賦值時,exports所使用的屬性和方法必須出現在module.exports,若屬性沒有在module.exports中定義的話,出現undefined,若方法沒有在module.exports中定義,會拋出TypeError錯誤。

例三 4.js、5.js

module.exports的對象、prototype、構造函數使用

// 4.jsvar a = require('./5.js'); // 若傳的是類,new一個對象var person = new a('Kylin',20); console.log(person.speak()); // my name is Kylin ,my age is 20// 若不需要在構造函數時初始化參數,直接調用方法/屬性// 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;// 若構造函數沒有傳入參數(name,age),直接傳入對象// module.exports = new Person('kylin',20);

說了這么多,其實建議就是,如果只是單一屬性或方法的話,就使用exports.屬性/方法。要是導出多個屬性或方法或使用對象構造方法,結合prototype等,就建議使用module.exports = {} 。文章有很多地方描述的可能不是很準確,提到的點也不夠全面,如果有不對的地方,還望斧正!

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海城市| 炉霍县| 云林县| 祁门县| 肇源县| 丹阳市| 会泽县| 伊金霍洛旗| 策勒县| 葫芦岛市| 远安县| 和顺县| 大余县| 扎鲁特旗| 阳曲县| 易门县| 格尔木市| 沐川县| 新绛县| 宝山区| 赫章县| 石渠县| 焉耆| 金溪县| 隆子县| 曲周县| 万安县| 三明市| 东阿县| 永城市| 内黄县| 南漳县| 滦平县| 玛曲县| 上蔡县| 西青区| 安吉县| 沐川县| 巴中市| 三台县| 清河县|