最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解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)這個,需要將期望導(dǎo)出的對象賦值給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導(dǎo)入方式去理解,關(guān)鍵有兩個變量(全局變量module.exports,局部變量exports)、一個返回值(module.exports)

function require(...) { 
 var module = { exports: {} };
 ((module, exports) => {
 // 你的被引入代碼 Start
 // var exports = module.exports = {}; (默認(rèn)都有的)
 function some_func() {};
 exports = some_func;
 // 此時,exports不再掛載到module.exports,
 // export將導(dǎo)出{}默認(rèn)對象
 module.exports = some_func;
 // 此時,這個模塊將導(dǎo)出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'); 
// 當(dāng)屬性和函數(shù)在module.exports都有定義時:
console.log(a.id); // module.exports的id 
console.log(a.func()); // module.exports的函數(shù)

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

// 當(dāng)函數(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的大哥,當(dāng)module.exports{}整體導(dǎo)出時會覆蓋exports的屬性和方法,

      2.注意,若只是將屬性/方法掛載在module.exports./exports.上時,exports.id=1module.exports.id=100,module.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、構(gòu)造函數(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

// 若不需要在構(gòu)造函數(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ù)沒有傳入?yún)?shù)(name,age),直接傳入對象
// module.exports = new Person('kylin',20);

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

總結(jié)

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

相關(guān)文章

  • SeaJS入門教程系列之使用SeaJS(二)

    SeaJS入門教程系列之使用SeaJS(二)

    這篇文章主要介紹了SeaJS入門教程系列之使用SeaJS,著重介紹了SeaJS的使用方法、關(guān)鍵方法的使用等,需要的朋友可以參考下
    2014-03-03
  • seajs中模塊的解析規(guī)則詳解和模塊使用總結(jié)

    seajs中模塊的解析規(guī)則詳解和模塊使用總結(jié)

    這篇文章主要介紹了seajs中模塊的解析規(guī)則詳解和模塊使用總結(jié),需要的朋友可以參考下
    2014-03-03
  • Seajs的學(xué)習(xí)筆記

    Seajs的學(xué)習(xí)筆記

    這篇文章主要介紹了Seajs的相關(guān)知識和和學(xué)習(xí)心得,適合剛接觸SeaJS的同學(xué),需要的朋友可以參考下
    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
  • sea.js常用的api簡易文檔

    sea.js常用的api簡易文檔

    現(xiàn)在使用sea.js的公司越來越多, 比如阿里巴巴,淘寶網(wǎng),百姓網(wǎng),支付寶,有道云筆記等。模塊化的javascript開發(fā)帶來了可維護(hù)、可擴(kuò)展性,尤其在多人協(xié)作開發(fā)的時候不用再擔(dān)心文件依賴和函數(shù)命名沖突的問題,本文給大家分享了sea.js常用的api簡易文檔,下面來一起看看吧
    2016-11-11
  • seajs中模塊依賴的加載處理實例分析

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

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

    seajs模塊壓縮問題與解決方法實例分析

    這篇文章主要介紹了seajs模塊壓縮問題與解決方法,結(jié)合實例形式分析了seajs模塊壓縮過程中出現(xiàn)的問題及相應(yīng)的解決方法,需要的朋友可以參考下
    2017-10-10
  • Seajs是什么及sea.js 由來,特點以及優(yōu)勢

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

    這篇文章主要介紹了Seajs的相關(guān)知識和和學(xué)習(xí)心得,適合剛接觸SeaJS的同學(xué),需要的朋友可以參考下,有更好的新手教程或文檔,歡迎推薦、分享
    2016-10-10
  • Seajs源碼詳解分析

    Seajs源碼詳解分析

    近幾年前端工程化越來越完善,打包工具也已經(jīng)是前端標(biāo)配了,像seajs這種老古董早已停止維護(hù),這是一篇細(xì)細(xì)品味Seajs源碼的文章,看完一定受益匪淺
    2019-04-04
  • SeaJS入門教程系列之SeaJS介紹(一)

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

    這篇文章主要介紹了SeaJS入門教程,講述了SeaJS的由來,JavaScript傳統(tǒng)開發(fā)模式和模塊化開發(fā)的對比,需要的朋友可以參考下
    2014-03-03

最新評論

建湖县| 隆尧县| 阳东县| 加查县| 武山县| 罗山县| 噶尔县| 四平市| 古丈县| 郁南县| 青河县| 台南市| 滦南县| 亚东县| 辛集市| 吴忠市| 同德县| 广汉市| 宁化县| 抚顺市| 平远县| 九龙县| 富锦市| 柏乡县| 娄烦县| 永泰县| 融水| 盘山县| 大渡口区| 辽宁省| 阿鲁科尔沁旗| 墨竹工卡县| 深圳市| 辛集市| 绍兴市| 成都市| 谷城县| 宁明县| 枝江市| 祁门县| 深水埗区|