新包装器如何在JavaScript中工作?

时间:2012-08-06 21:25:53

标签: javascript underscore.js

在underscore.js:

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

简单地说,这个函数返回什么?

2 个答案:

答案 0 :(得分:1)

Itwrapper constructor的包装函数,允许您在没有new keyword的情况下使用下划线。调用下划线将始终返回一个新的wrapper实例。


顺便说一下,wrapper功能已在this commit中删除。 _函数本身就是构造函数,请参阅Understanding the declaration of the underscore in _.js?以获取解释。

答案 1 :(得分:0)

简单地说,它是'包装器'的构造函数,使事情变得更容易

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

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

我认为在underscore.js之后,它是为了让你的编码保持整洁和简单:)