我正在学习使用下划线的 Backbone。
在一些示例中,我看到初始化代码来创建一个空的子数组,如下所示:
// inside a constructor function for a view object that will be extended:
this.children = _([]);
_
上面调用的 Underscore 函数是在 Underscore.js 顶部附近定义的:
// Create a safe reference to the Underscore object for use below.
var _ = function(obj) {
if (obj instanceof _) return obj;
if (!(this instanceof _)) return new _(obj);
this._wrapped = obj;
};
在调试器中单步执行显示return new _(obj)
首先调用了该函数,因此再次调用该函数并最终this._wrapped = obj
执行。 this
似乎是指_
.
我很困惑。为什么不一开始就说this.children = []
呢?