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

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

本文关键字:什么 区别 之间 forEach each 下划线      更新时间:2023-10-18

我研究Backbone n下划线
有人帮我。
我不知道模型是未定义的_.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返回一个在todos.models 中保存模型数组的代理对象

此处的工作代码