js设计模式:迭代器
2019-08-29 本文已影响0人
中华小灰灰
1.内部迭代器模式
简单的来说就是forEach的实现
var runEach = funtion(obj,callback) {
var value;
if (isArray) {
for(var i =0;i<obj.length;i++) {
value = callback.call(obj[i], i, obj[i]);
}
if(value == false) break;
}
}
2.外部迭代器
用next迭代直到完成,通常由next, isDone等方法
var Itaretor = function(obj) {
var index = 0;
var next = function() {
index +=1;
}
var isDone = function() {
return index >= obj.length;
}
var getCurrentItem = function() {
return obj[index]
}
return {
next,
isDone,
getCurrentItem,
}
}