这是从一开始带注释的 _.js 来源 http://documentcloud.github.io/underscore/docs/underscore.html。尽管我可以尝试,但我的 JavaScript 能力还不够高,无法理解这里发生的事情。我希望有人能够给出真正的一步一步的解释。我真的有no idea尽管我理解每个单独的表达式,但除了以某种方式设置 _ 供使用之外,下面的代码还做了什么。
var _ = function(obj) {
if (obj instanceof _) return obj;
if (!(this instanceof _)) return new _(obj);
this._wrapped = obj;
};
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = _;
}
exports._ = _;
} else {
root._ = _;
}
var _ = function(obj) {
// Either serve as the identity function on `_` instances,
// ... or instantiate a new `_` object for other input.
// If an `_` instance was passed, return it.
if (obj instanceof _) return obj;
// If someone called `_(...)`, rather than `new _(...)`,
// ... return `new _(...)` to instantiate an instance.
if (!(this instanceof _)) return new _(obj);
// If we are instantiating a new `_` object with an underlying,
// ... object, set that object to the `_wrapped` property.
this._wrapped = obj;
};
// If there is an exports value (for modularity)...
if (typeof exports !== 'undefined') {
// If we're in Node.js with module.exports...
if (typeof module !== 'undefined' && module.exports) {
// Set the export to `_`
exports = module.exports = _;
}
// Export `_` as `_`
exports._ = _;
} else {
// Otherwise, set `_` on the global object, as set at the beginning
// ... via `(function(){ var root = this; /* ... */ }())`
root._ = _;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)