下划线 _.each 和 forEach 有什么区别?

What is the difference between underscore _.each and forEach?

我研究Backbonen下划线
有人帮帮我。
我不知道模型未定义 _.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.

为什么模型未定义???

    /*underscoreJS  model is undefined...*/
    _.each(todos, function (model) {
        console.log(model.get('title'));
    });

对于下划线,你需要这样做:

 _.each(todos.models, function(model) {
   console.log(model.get('title'));
 });

你这样做的原因

 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 在 todos.models

中保存模型数组的代理对象

工作代码here