以下是源碼信息
// Export the Underscore object for **Node.js**, with
// backwards-compatibility for their old module API. If we're in
// the browser, add `_` as a global object.
// (`nodeType` is checked to ensure that `module`
// and `exports` are not HTML elements.)
if (typeof exports != 'undefined' && !exports.nodeType) {
if (typeof module != 'undefined' && !module.nodeType && module.exports) {
exports = module.exports = _;
}
exports._ = _;
} else {
root._ = _;
}
這段代碼應(yīng)該是針對在nodejs環(huán)境中將_
的方法掛載到exports._
上,但不太懂這段代碼具體邏輯:
typeof exports != 'undefined' && !exports.nodeType
為什么不使用嚴格不等于!==
或直接用隱式轉(zhuǎn)換exports && !exports.nodeType
;
exports.nodeType
和 module.nodeType
是什么?;
exports = module.exports = _;
這行代碼如何理解?。
其實上面的英文注釋已經(jīng)解釋了許多
這段代碼是用于在nodejs環(huán)境中暴露_
方法
//這里這個if else是為了確定當(dāng)前環(huán)境是node環(huán)境還是瀏覽器環(huán)境
//typeof exports的結(jié)果必然是String類型,因此不使用嚴格不等于也可以
//至于為什么不使用隱式轉(zhuǎn)換,應(yīng)該是為了代碼語義更明確,就是想判斷不是undefined類型(存疑)
if (typeof exports != 'undefined' && !exports.nodeType) {
//nodeType是為了確定該值不是html dom元素
if (typeof module != 'undefined' && !module.nodeType && module.exports) {
//node環(huán)境下exports = module.exports = {},exports是對module.exports的引用
//module.exports = _ ,注意到_其實是個函數(shù)(這段代碼上面定義了)
//這切斷了exports和module.exports的聯(lián)系,只能被module.exports輸出,不能被exports輸出
//所以需要exports = module.exports,重新建立起 exports 對 module.exports 的引用
exports = module.exports = _;
}
//兼容對module.exports支持不好的舊的commonJS規(guī)范
//引用的時候可以 var _ = require("underscore")._
exports._ = _;
} else {
//瀏覽器環(huán)境,_方法掛載到window上
root._ = _;
}
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團,成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達內(nèi)教育集團成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負責(zé)iOS教學(xué)及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。