How does the following javascript code create a safe reference to the underscore object?
-
I was going through the annotated source code of underscore.js when I saw this snippet of code: var _ = function(obj) { if (obj instanceof _) return obj; if (!(this instanceof _)) return new _(obj); this._wrapped = obj; }; The annotation to this section reads "Create a safe reference to the Underscore object for use below." But how does this work? I'm new to javascript, a line by line explanation would be great. Thanks! via http://underscorejs.org/docs/underscore.html#section-9
-
Answer:
By the code you've posted what i understand is that it's just making sure if the obj being passed to it is of type _. If not they are converting it into an underscore object. The primary purpose of which is to ascertain that henceforth the variable is of this type.
Clive Verghese at Quora Visit the source
Related Q & A:
- how to Create a Java Package from MATLAB Code?Best solution by Stack Overflow
- How to create a underscore template dynamically?Best solution by Stack Overflow
- How to encrypt JavaScript code?Best solution by Stack Overflow
- How to create a loop for below code?Best solution by WordPress
- How to create a flexible object?
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.