據Underscore.JS源(https://github.com/jashkenas/underscore/blob/master/underscore.js):爲什麼Underscore.js的chain()方法不是懶惰?
// Start chaining a wrapped Underscore object.
chain: function() {
this._chain = true;
return this;
},
// Extracts the result from a wrapped and chained object.
value: function() {
return this._wrapped;
}
鏈()和值()函數是用於下劃線對象只是簡單的包裝。
所以,如果我用以下結構:
_.chain(someCollection)
.map(function1)
.map(function2)
.map(function3)
.value()
下劃線將創建兩個中間集合,並且將執行三個枚舉。
爲什麼chain()和value()方法沒有像LINQ實現它的方法那樣被執行爲懶惰評估?例如,這條鏈可以被看作:
_.chain(someCollection)
.map(function(x){
return function3(function2(function1(x)));
})
.value();
這種實現有沒有JS相關的問題?