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

詳解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ū)別,需要的朋友可以參考借鑒,一起來看看吧。

一、官方解釋

因?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);

從事例一中,可以看出來

      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沒有定義,函數(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());  // 報(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的順序,誰在后,就是最后的值

      3.若exportsmodule.exports同時(shí)賦值時(shí),exports所使用的屬性和方法必須出現(xiàn)在module.exports,若屬性沒有在module.exports中定義的話,出現(xiàn)undefined,若方法沒有在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ù)沒有傳入?yún)?shù)(name,age),直接傳入對(duì)象
// module.exports = new Person('kylin',20);

說了這么多,其實(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í)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

相關(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)知識(shí)和和學(xué)習(xí)心得,適合剛接觸SeaJS的同學(xué),需要的朋友可以參考下
    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ū)別,需要的朋友可以參考借鑒,一起來看看吧。
    2017-02-02
  • sea.js常用的api簡(jiǎn)易文檔

    sea.js常用的api簡(jiǎn)易文檔

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

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

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

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

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

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

    這篇文章主要介紹了Seajs的相關(guān)知識(shí)和和學(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ā)的對(duì)比,需要的朋友可以參考下
    2014-03-03

最新評(píng)論

鹿泉市| 鄂托克前旗| 通山县| 方山县| 确山县| 永仁县| 凤城市| 淄博市| 连州市| 三台县| 江安县| 梓潼县| 临武县| 涪陵区| 商丘市| 武义县| 孟村| 湟源县| 合川市| 嵊泗县| 香河县| 定边县| 汝州市| 逊克县| 盐津县| 太和县| 二手房| 仙桃市| 藁城市| 郯城县| 搜索| 麟游县| 佳木斯市| 土默特右旗| 灵丘县| 理塘县| 保亭| 新干县| 河东区| 衡山县| 马关县|