enumeration - Writing an enumerator for a JavaScript object -
i'm trying understand how enumerator javascript object can written. came across piece of sharepoint code items of sp list being obtained on client side, , code similar 1 below being performed on sharepoint object.
var enumerator = list.getenumerator(); while(enumerator.movenext()) { var currentitem = enumerator.getcurrent(); // actions on currentitem }
if write custom object iterated upon using enumerator above, how look? attempt, 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 me understand how getenumerator()
written function prototype applicable enumerable object?
try below:
var list = function() { // these local variables accessed 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 return this.enumerator() this.enumerator return this.enumerator; } }; }; var enumerator = list().getenumerator(); while(enumerator.movenext()) { var currentitem = enumerator.getcurrent(); alert(currentitem); }
Comments
Post a Comment