欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Sea.js中Module.exports和exports的區(qū)別

 更新時間:2017年02月12日 11:42:13   作者:信鑫  
最近在看Seajs時,看到了exports.doSomething和module.exports,想對這兩者的區(qū)別一探究竟。所以下面這篇文章主要介紹了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 = {}; ,使exportsmodule.exports都指向一個空對象。

      2.module是全局內(nèi)置對象,exports是被var創(chuàng)建的局部對象。

      3.module.exportsexports所指向的內(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=1module.exports.id=100module.exports.id=function(){}exports.id=function(){} ,最后id的值取決于exports.idmodule.exports.id的順序,誰在后,就是最后的值

      3.若exportsmodule.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)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

相關文章

  • seajs模塊之間依賴的加載以及模塊的執(zhí)行

    seajs模塊之間依賴的加載以及模塊的執(zhí)行

    做前端項目已經(jīng)離不開SeaJS了,現(xiàn)在的很多的網(wǎng)站都用了SeaJS,對這個JS模塊加載器原理的越來越感興趣。這篇文章我們來一起學習seajs模塊之間依賴的加載以及模塊的執(zhí)行,感興趣的朋友們下面來一起看看吧。
    2016-10-10
  • SeaJS入門教程系列之完整示例(三)

    SeaJS入門教程系列之完整示例(三)

    這篇文章主要介紹了SeaJS入門教程系列之完整示例,演示了一個完整的SeaJS開發(fā)例子,需要的朋友可以參考下
    2014-03-03
  • SeaJS入門教程系列之SeaJS介紹(一)

    SeaJS入門教程系列之SeaJS介紹(一)

    這篇文章主要介紹了SeaJS入門教程,講述了SeaJS的由來,JavaScript傳統(tǒng)開發(fā)模式和模塊化開發(fā)的對比,需要的朋友可以參考下
    2014-03-03
  • 詳解Sea.js中Module.exports和exports的區(qū)別

    詳解Sea.js中Module.exports和exports的區(qū)別

    最近在看Seajs時,看到了exports.doSomething和module.exports,想對這兩者的區(qū)別一探究竟。所以下面這篇文章主要介紹了Sea.js中Module.exports和exports的區(qū)別,需要的朋友可以參考借鑒,一起來看看吧。
    2017-02-02
  • Seajs源碼詳解分析

    Seajs源碼詳解分析

    近幾年前端工程化越來越完善,打包工具也已經(jīng)是前端標配了,像seajs這種老古董早已停止維護,這是一篇細細品味Seajs源碼的文章,看完一定受益匪淺
    2019-04-04
  • seajs學習之模塊的依賴加載及模塊API的導出

    seajs學習之模塊的依賴加載及模塊API的導出

    SeaJS是一個遵循 CommonJS 規(guī)范的模塊加載框架,可用來輕松愉悅地加載任意JavaScript模塊和css模塊樣式。SeaJS接口和方法也非常少,SeaJS 就兩個核心:模塊定義和模塊的加載及依賴關系。本文將詳細介紹模塊的依賴加載及模塊API的導出,有需要的朋友們可以參考借鑒。
    2016-10-10
  • Vue實現(xiàn)圖片懶加載的多種方法詳解

    Vue實現(xiàn)圖片懶加載的多種方法詳解

    圖片懶加載是前端開發(fā)中一項重要的性能優(yōu)化策略,它能夠明顯縮短頁面加載時間,提升用戶的瀏覽體驗,在本文中,我們將深入探討在Vue開發(fā)時候如何實現(xiàn)圖片懶加載,同時探討多種實現(xiàn)途徑,需要的朋友可以參考下
    2023-08-08
  • seaJs的模塊定義和模塊加載淺析

    seaJs的模塊定義和模塊加載淺析

    這篇文章主要介紹了seaJs的模塊定義和模塊加載淺析,以及模塊間的依賴關系等問題,需要的朋友可以參考下
    2014-06-06
  • Seajs是什么及sea.js 由來,特點以及優(yōu)勢

    Seajs是什么及sea.js 由來,特點以及優(yōu)勢

    這篇文章主要介紹了Seajs的相關知識和和學習心得,適合剛接觸SeaJS的同學,需要的朋友可以參考下,有更好的新手教程或文檔,歡迎推薦、分享
    2016-10-10
  • seajs中模塊依賴的加載處理實例分析

    seajs中模塊依賴的加載處理實例分析

    這篇文章主要介紹了seajs中模塊依賴的加載處理,結合實例形式分析了seajs模塊依賴與加載的原理、相關注意事項與使用方法,需要的朋友可以參考下
    2017-10-10

最新評論