0

In the context of underscore.js:

// Create a safe reference to the Underscore object for use below.
   var _ = function(obj) { return new wrapper(obj); };

Simply put, what does this function return?

putvande
  • 15,068
  • 3
  • 34
  • 50
Sean
  • 779
  • 6
  • 18
  • it returns a new instance of the wrapper type. Read more about javascript's object model here: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Details_of_the_Object_Model – Esailija Aug 06 '12 at 21:27
  • Which is an object oriented bit of code, based on some other javascript you would likely have somewhere. That has the wrapper object/class in it. – chris Aug 06 '12 at 21:28

2 Answers2

1

It is a wrapper function for the wrapper constructor to allow you using underscore without the new keyword. Calling underscore will always return a new wrapper instance.


Btw, the wrapper function has been removed in this commit. The _ function itself is the constructor now, see Understanding the declaration of the underscore in _.js? for explanation.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

simply put, it's a constructor for 'wrapper' to make things a big easier for you

// this allows you to do things such as:
var a = _({/*object*/});

// rether than something like:
var a = new wrapper({/*object*/});

I think in the since of underscore.js it's to keep your coding tiddy and simple :)

SReject
  • 3,774
  • 1
  • 25
  • 41