admin管理员组文章数量:1432566
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?
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?
3 Answers
Reset to default 3Try 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.
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();
Thanks to pointers from @xdazz and @Juan Mendes, I was able to work out the enumerator function prototype:
Array.prototype extension:
Array.prototype.getEnumerator = Array.prototype.getEnumerator ||
function() {
var items = this;
var counter = -1;
var enumerator = {
moveNext: function() {
counter++;
return (counter < items.length);
},
getCurrent: function() {
return items[counter];
}
}
return enumerator;
};
Usage:
var list = ["A", "B", "C", "D", "E"];
var enumerator = list.getEnumerator(); // makes any array enumerable
while(enumerator.moveNext()) {
var currentItem = enumerator.getCurrent();
alert(currentItem);
}
JSFIDDLE DEMO
本文标签: enumerationWriting an enumerator for a JavaScript objectStack Overflow
版权声明:本文标题:enumeration - Writing an enumerator for a JavaScript object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745586468a2664936.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论