etc

iterator

Sumin Lim 2011. 10. 5. 15:35
반응형
function iterator(collection) {
    var index = 0;
    var length = collection.length;
    
    function next() {
        var item = collection[index++];
        next.hasNext = index < length;
        return item;
    }
    
    next.hasNext = index < length;
    return next;
}




(function () {
    var collection = [1, 2, 3, 4, 5];
    var next = iterator(collection);
    var result = [];
    while (next.hasNext) {
        result.push(next());
    }
    console.log(collection);
    console.log(result);
}());

반응형