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

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

 更新時(shí)間:2017年02月12日 11:42:13   作者:信鑫  
最近在看Seajs時(shí),看到了exports.doSomething和module.exports,想對(duì)這兩者的區(qū)別一探究竟。所以下面這篇文章主要介紹了Sea.js中Module.exports和exports的區(qū)別,需要的朋友可以參考借鑒,一起來(lái)看看吧。

一、官方解釋

因?yàn)镾eaJs和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對(duì)象是由模塊系統(tǒng)創(chuàng)建的。 有時(shí)這是難以接受的;許多人希望他們的模塊成為某個(gè)類的實(shí)例。 為了實(shí)現(xiàn)這個(gè),需要將期望導(dǎo)出的對(duì)象賦值給module.exports。 注意,將期望的對(duì)象賦值給exports會(huì)簡(jiǎn)單地重新綁定到本地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變量是在模塊的文件級(jí)別作用域內(nèi)有效的,它在模塊被執(zhí)行前被賦于 module.exports 的值。它有一個(gè)快捷方式,以便 module.exports.f = ... 可以被更簡(jiǎn)潔地寫成exports.f = ...。 注意,就像任何變量,如果一個(gè)新的值被賦值給exports,它就不再綁定到module.exports(其實(shí)是exports.屬性會(huì)自動(dòng)掛載到?jīng)]有命名沖突的module.exports.屬性)

require

從require導(dǎo)入方式去理解,關(guān)鍵有兩個(gè)變量(全局變量module.exports,局部變量exports)、一個(gè)返回值(module.exports)

function require(...) { 
 var module = { exports: {} };
 ((module, exports) => {
 // 你的被引入代碼 Start
 // var exports = module.exports = {}; (默認(rèn)都有的)
 function some_func() {};
 exports = some_func;
 // 此時(shí),exports不再掛載到module.exports,
 // export將導(dǎo)出{}默認(rèn)對(duì)象
 module.exports = some_func;
 // 此時(shí),這個(gè)模塊將導(dǎo)出some_func對(duì)象,覆蓋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);

從事例一中,可以看出來(lái)

      1.每個(gè)js文件一創(chuàng)建,都有一個(gè)var exports = module.exports = {}; ,使exportsmodule.exports都指向一個(gè)空對(duì)象。

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

      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'); 
// 當(dāng)屬性和函數(shù)在module.exports都有定義時(shí):
console.log(a.id); // module.exports的id 
console.log(a.func()); // module.exports的函數(shù)

// 當(dāng)屬性在module.exports沒(méi)有定義,函數(shù)在module.exports有定義
console.log(a.id2); // undefined 
console.log(a.func()); // module.exports的函數(shù)

// 當(dāng)函數(shù)在module.exports沒(méi)有定義,屬性在module.exports有定義
console.log(a.id);    // module.exports的id 
console.log(a.func2());  // 報(bào)錯(cuò)了 TypeError: a.func2 is not a function 

由例二可以知道:

      1.module.exports像是exports的大哥,當(dāng)module.exports{}整體導(dǎo)出時(shí)會(huì)覆蓋exports的屬性和方法,

      2.注意,若只是將屬性/方法掛載在module.exports./exports.上時(shí),exports.id=1module.exports.id=100,module.exports.id=function(){}exports.id=function(){} ,最后id的值取決于exports.idmodule.exports.id的順序,誰(shuí)在后,就是最后的值

      3.若exportsmodule.exports同時(shí)賦值時(shí),exports所使用的屬性和方法必須出現(xiàn)在module.exports,若屬性沒(méi)有在module.exports中定義的話,出現(xiàn)undefined,若方法沒(méi)有在module.exports中定義,會(huì)拋出TypeError錯(cuò)誤。

例三 4.js、5.js

module.exports的對(duì)象、prototype、構(gòu)造函數(shù)使用

// 4.js
var a = require('./5.js'); 
// 若傳的是類,new一個(gè)對(duì)象
var person = new a('Kylin',20); 
console.log(person.speak()); // my name is Kylin ,my age is 20

// 若不需要在構(gòu)造函數(shù)時(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;

// 若構(gòu)造函數(shù)沒(méi)有傳入?yún)?shù)(name,age),直接傳入對(duì)象
// module.exports = new Person('kylin',20);

說(shuō)了這么多,其實(shí)建議就是,如果只是單一屬性或方法的話,就使用exports.屬性/方法。要是導(dǎo)出多個(gè)屬性或方法或使用對(duì)象構(gòu)造方法,結(jié)合prototype等,就建議使用module.exports = {} 。文章有很多地方描述的可能不是很準(zhǔn)確,提到的點(diǎn)也不夠全面,如果有不對(duì)的地方,還望斧正!

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。

相關(guān)文章

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

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

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

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

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

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

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

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

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

    Seajs源碼詳解分析

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

    seajs學(xué)習(xí)之模塊的依賴加載及模塊API的導(dǎo)出

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

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

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

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

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

    Seajs是什么及sea.js 由來(lái),特點(diǎn)以及優(yōu)勢(shì)

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

    seajs中模塊依賴的加載處理實(shí)例分析

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

最新評(píng)論