[英]What is the difference between underscore _.each and forEach?
I study Backbone n underscore. 我研究Backbone n下划线。
somebody help me. 来人帮帮我。
I don't know that model is undefined of _.each(). 我不知道该模型未定义_.each()。
var todos = new Backbone.Collection();
todos.add([
{ title: 'go to Belgium.', completed: false },
{ title: 'go to China.', completed: false },
{ title: 'go to Austria.', completed: true }
]);
// iterate over models in the collection
todos.forEach(function(model){
console.log(model.get('title'));
});
// Above logs:
// go to Belgium.
// go to China.
// go to Austria.
Why model is undefined??? 为什么模型未定义?
/*underscoreJS model is undefined...*/
_.each(todos, function (model) {
console.log(model.get('title'));
});
For underscore you need to do like this: 对于下划线,您需要这样做:
_.each(todos.models, function(model) {
console.log(model.get('title'));
});
Reason when you do 你这么做的原因
var todos = new Backbone.Collection();
todos.add([{
title: 'go to Belgium.',
completed: false
}, {
title: 'go to China.',
completed: false
}, {
title: 'go to Austria.',
completed: true
}]);
Backbone returns a proxy object which hold the model array in todos.models
Backbone返回一个代理对象,该对象将模型数组保存在todos.models
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.