首先为了达到可以用_
调用的目的,我们需要先定义出_
,也即:
var root = (typeof self == 'object' && self.self == self && self) ||
(typeof global == 'object' && global.global == global && global)||
this || {};
//在浏览器中,除了window属性,我们也可以通过self属性直接访问到Window对象,同时self还可以支持Web Worker
//Node环境中全局变量为 global
//node vm(沙盒模型) 中不存在window,也不存在global变量,但我们却可以通过this访问到全局变量
//在微信小程序中,window和global都是undefined,加上强制使用严格模式,this为undefined,就多了{}
var _ = function(obj){
if(obj instanceof _){
return obj;
}
if(!(this instanceof _)){ // 此处为了实现面向对象风格调用,可以暂时不管
return new _(obj);
}
this._wrapped = obj;
};
//exports.nodeType 防止<div id="exports"></div>产生的window.exports全局变量。
if(typeof exports != 'undefined' && !exports.nodeType){
if((typeof module != 'undefined' && !module.nodeType && module.exports)){
//在nodeJs中,exports是module.exports 的一个引用,当你使用了module.exports = function(){}
//实际上覆盖了module.exports,但是exports并未发生改变,为了避免后面在修改exports而导致不能正确输出
//写成这样,将两者保持统一。
exports = module.exports = _;
}
exports._ = _;
}else{
//?不太懂部分,将_挂到全局属性_上
root._ = _;
}