addyosmani / addyosmani/backbone-fundamentals

Collection seems chainable in the Chainable API example

未关闭
#669 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Rich Text Format
星标
9.2k
派生
1.4k
PR 合并指标
30 天内没有已合并 PR

描述

In Chainable API example I saw this:

``` javascript
var collection = new Backbone.Collection([
{ name: 'Tim', age: 5 },
{ name: 'Ida', age: 26 },
{ name: 'Rob', age: 55 }
]);

var filteredNames = collection.chain() // start chain, returns wrapper around collection's models
.filter(function(item) { return item.get('age') > 10; }) // returns wrapped array excluding Tim
.map(function(item) { return item.get('name'); }) // returns wrapped array containing remaining names
.value(); // terminates the chain and returns the resulting array

console.log(filteredNames); // logs: ['Ida', 'Rob']
```

Therefore I tested to just use the collection without the chain() method. It seems that it worked well. I ran this in the console:

``` javascript
var collection = new Backbone.Collection([
{ name: 'Tim', age: 5 },
{ name: 'Ida', age: 26 },
{ name: 'Rob', age: 55 }
]);

collection
.filter(function(item) { return item.get('age') > 10; })
.map(function(item) { return item.get('name'); }) ;
```

and it returns `['Ida', 'Rob']`.

I'm wondering why? It seems that the chain() method is not necessary? Thanks!

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。