这个"each"方法的来源?
Source of this "each" method?
在 this page Backbone 文档的第一个示例中,messages
对象使用方法 each
。这里用的是哪个框架的方法each
:underscore.js,backbone.js,还是jquery?
var MessageList = Backbone.View.extend({
initialize: function() {
var messages = this.collection;
messages.on("reset", this.render, this);
messages.on("add", this.addMessage, this);
messages.on("remove", this.removeMessage, this);
messsages.each(this.addMessage, this);
}
});
我认为
1.it 不能是下划线方法,因为它使用 _
的语法
2.it 不能是 jquery 方法,因为它使用 $
的语法
3.it 不能是 backbone 方法,因为它使用下划线方法
"Backbone proxies to Underscore.js to provide 46 iteration functions
on Backbone.Collection. They aren't all documented here, but you can
take a look at the Underscore documentation for the full details…"
如果您查看 backbone 文档,您会看到 backbone 中使用的下划线方法列表。其中之一是 each
方法,列在 Underscore Methods (46) 段落下。并在下划线 documentation it's explicitly stated that the each method is actually an alias of Javascript forEach
方法中。它有什么作用?
下划线描述:"It iterates over a list of elements, yielding each in turn to an iteratee function."
MDN 描述:"forEach() executes the provided callback once for each element present in the array in ascending order. It is not invoked for index properties that have been deleted or are uninitialized (i.e. on sparse arrays)."
要回答你的问题为什么在 backbone 中使用的 each
方法之前没有下划线符号依赖于下划线 source 代码中的这一小节:
var _ = function(obj) {
if (obj instanceof _) return obj;
if (!(this instanceof _)) return new _(obj);
this._wrapped = obj;
};
此函数的作用是,如果尚未实例化另一个对象,则用下划线实例化一个新对象。如果已经有一个带有 _
叹息的对象,那么它只是 returns 它。
几乎所有下划线方法都可以直接用作集合方法。
在 this page Backbone 文档的第一个示例中,messages
对象使用方法 each
。这里用的是哪个框架的方法each
:underscore.js,backbone.js,还是jquery?
var MessageList = Backbone.View.extend({
initialize: function() {
var messages = this.collection;
messages.on("reset", this.render, this);
messages.on("add", this.addMessage, this);
messages.on("remove", this.removeMessage, this);
messsages.each(this.addMessage, this);
}
});
我认为
1.it 不能是下划线方法,因为它使用 _
2.it 不能是 jquery 方法,因为它使用 $
3.it 不能是 backbone 方法,因为它使用下划线方法
"Backbone proxies to Underscore.js to provide 46 iteration functions on Backbone.Collection. They aren't all documented here, but you can take a look at the Underscore documentation for the full details…"
如果您查看 backbone 文档,您会看到 backbone 中使用的下划线方法列表。其中之一是 each
方法,列在 Underscore Methods (46) 段落下。并在下划线 documentation it's explicitly stated that the each method is actually an alias of Javascript forEach
方法中。它有什么作用?
下划线描述:"It iterates over a list of elements, yielding each in turn to an iteratee function."
MDN 描述:"forEach() executes the provided callback once for each element present in the array in ascending order. It is not invoked for index properties that have been deleted or are uninitialized (i.e. on sparse arrays)."
要回答你的问题为什么在 backbone 中使用的 each
方法之前没有下划线符号依赖于下划线 source 代码中的这一小节:
var _ = function(obj) {
if (obj instanceof _) return obj;
if (!(this instanceof _)) return new _(obj);
this._wrapped = obj;
};
此函数的作用是,如果尚未实例化另一个对象,则用下划线实例化一个新对象。如果已经有一个带有 _
叹息的对象,那么它只是 returns 它。
几乎所有下划线方法都可以直接用作集合方法。