迭代与underscore.js对象 [英] Iterating objects with underscore.js

查看:124
本文介绍了迭代与underscore.js对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我学习了Backbone.js的,目前遍历一些型号与下面的例子。第一代码段的工作原理,当基于underscore.js另一个没有。为什么呢?

So, I am learning out backbone.js and are currently iterating over some models in a view with the below example. The first snippet works, when the other underscore.js-based one doesn't. Why?

// 1: Working
this.collection.each(function(model){ console.log(model.get("description")); });

// 2: Not working       
_.each(this.collection, function(model){ console.log(model.get("description")); });

我是什么做错了,因为我看不到我自己?

What am I doing wrong, as I can't see it by myself?

推荐答案

this.collection 是一个实例,而 this.collection.each 是迭代这是一个集合实例的 .models 属性在幕后正确的对象的方法。

this.collection is an instance while this.collection.each is a method that iterates the proper object under the covers which is the .models property of a collection instance.

与此说,你可以试试:

_.each(this.collection.models, function(model){ console.log(model.get("description")); });

这是因为 this.collection.each 完全没有意义的是,做类似的功能:

Which is completely pointless as this.collection.each is a function that does similar to:

function(){
return _.each.apply( _, [this.models].concat( [].slice.call( arguments ) ) );
}

所以你还不如用 this.collection.each ; P

这篇关于迭代与underscore.js对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆