在 underscore.js 的上下文中:
// Create a safe reference to the Underscore object for use below.
var _ = function(obj) { return new wrapper(obj); };
简单来说,这个函数返回什么?
在 underscore.js 的上下文中:
// Create a safe reference to the Underscore object for use below.
var _ = function(obj) { return new wrapper(obj); };
简单来说,这个函数返回什么?
它是wrapper构造函数的包装函数,允许您使用不带new关键字的下划线。调用下划线将始终返回一个新wrapper实例。
顺便说一句,该wrapper功能已在此提交中删除。_函数本身现在是构造函数,请参阅Understanding the declaration of the underscore in _.js? 解释。
简单地说,它是 'wrapper' 的构造函数,让你的事情变得更容易
// this allows you to do things such as:
var a = _({/*object*/});
// rether than something like:
var a = new wrapper({/*object*/});
我认为从 underscore.js 开始,它是为了让你的编码保持整洁和简单:)