How to create deep object with dynamic name in Javascript?

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

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

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.