Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing an enumerator for a JavaScript object

I'm trying to understand how an enumerator for a JavaScript object can be written. I came across a certain piece of Sharepoint code where items of a SP List were being obtained on the client side, and code similar to the one below was being performed on a Sharepoint object.

var enumerator = list.getEnumerator();
while(enumerator.moveNext()) {
    var currentItem = enumerator.getCurrent();
    // actions on currentItem
}

If I could write a custom object that could be iterated upon using an enumerator as above, how would it look? This is my attempt, but it doesn't work:

var list = {
    items: ["A", "B", "C", "D", "E"],
    counter: -1,
    enumerator: {
        moveNext: function() {
            counter++; // outer object's variable not accessible
            if(counter >= items.length)
                return false;
            else
                return true;
        },
        getCurrent: function() {
            return items[counter]; // outer object's variable not accessible
        }
    },
    getEnumerator: function() { 
        return this.enumerator(); 
    }
}

var enumerator = list.getEnumerator();
while(enumerator.moveNext()) {
    var currentItem = enumerator.getCurrent();
    alert(currentItem);
}

Can you help me understand how getEnumerator() could be written as a function prototype that is applicable to any enumerable object?

like image 638
SNag Avatar asked Apr 13 '26 11:04

SNag


2 Answers

Try below:

var list = function() {
    // these local variables could be accessed by inner functions.
    var items = ["A", "B", "C", "D", "E"],
        counter =  -1;
    return {
        enumerator: {
            moveNext: function() {
                counter++; 
                return counter < items.length;
            },
            getCurrent: function() {
                return items[counter]; 
            }
        },
        getEnumerator: function() { 
            // here should not be return this.enumerator() but just this.enumerator
            return this.enumerator; 
        }
    };
};

var enumerator = list().getEnumerator();
while(enumerator.moveNext()) {
    var currentItem = enumerator.getCurrent();
    alert(currentItem);
}

THE JSFIDDLE DEMO.

like image 141
xdazz Avatar answered Apr 16 '26 01:04

xdazz


You can access the variables using list.

var list = {
    items: ["A", "B", "C", "D", "E"],
    counter: -1,
    enumerator: {
        moveNext: function() {
            list.counter++; // outer object's variable not accessible
            if(list.counter >= list.items.length)
                return false;
            else
                return true;
        },
        getCurrent: function() {
            return list.items[list.counter]; // outer object's variable not accessible
        }
    },
    getEnumerator: function() { 
        return list.enumerator; 
    }
}

Or you can make a function to create instances of it

function makeList(items) {
    var list = {
        items: items
        counter: -1,
        enumerator: {
            moveNext: function() {
                list.counter++; // outer object's variable not accessible
                if(list.counter >= list.items.length)
                    return false;
                else
                    return true;
            },
            getCurrent: function() {
                return list.items[list.counter];
            }
    },
    getEnumerator: function() { 
        return list.enumerator; 
    }
    return list;
}


var enumerator = makeList(["A", "B", "C", "D", "E"]).getEnumerator();
like image 22
Juan Mendes Avatar answered Apr 16 '26 02:04

Juan Mendes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!