admin管理员组文章数量:1389749
This is from the beginning of the annotated source of _.js. Try though I may, my JavaScript abilities are not at a high enough level to understand what's going on here. I'm hoping someone can give a real step by step explanation. I really have literally no idea what the code below does besides somehow setting up the _ for use, despite that I understand each individual expression.
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._ = _;
}
This is from the beginning of the annotated source of _.js. Try though I may, my JavaScript abilities are not at a high enough level to understand what's going on here. I'm hoping someone can give a real step by step explanation. I really have literally no idea what the code below does besides somehow setting up the _ for use, despite that I understand each individual expression.
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._ = _;
}
Share
Improve this question
asked Jul 15, 2013 at 18:12
temporary_user_nametemporary_user_name
37.2k48 gold badges160 silver badges243 bronze badges
4
- My Question, why does it matter what it does? – Shawn31313 Commented Jul 15, 2013 at 18:14
- 4 ....trying to learn and understand advanced coding techniques? – temporary_user_name Commented Jul 15, 2013 at 18:14
-
Okay, fair enough. Well first off, you need to know what
instanceof
does. Theinstanceof
operator tests whether an object has in its prototype chain the prototype property of a constructor. More about it here: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Shawn31313 Commented Jul 15, 2013 at 18:19 - 1 Patterns that check context before exposing are called universal module definitions. See here too. – ryanve Commented Jul 15, 2013 at 18:34
2 Answers
Reset to default 9var _ = 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._ = _;
}
Well, the lower snippet is quite unrelated. Basically it's exporting the _
from the closure to the global scope, or using a module definition system if available. No great deal, and nothing to care about if you don't use modules.
The _
function shouldn't be that hard to understand. You need to know
- What is the underscore function for?
- What does the
new
operator do? - How does the
instanceof
operator work?
So what it does:
- If the argument is an Underscore instance, return it unchanged.
- If the function is not called as a constructor (when the
this
context is not an Underscore instance) then do so and return that - Else wrap the argument in the current instance
本文标签: javascriptUnderstanding the declaration of the underscore in jsStack Overflow
版权声明:本文标题:javascript - Understanding the declaration of the underscore in _.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744741854a2622655.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论