-
Notifications
You must be signed in to change notification settings - Fork 35
Enumerator
Jing Lu edited this page Jun 17, 2013
·
13 revisions
ReoScript support to enumerate over an object which implementing the IEnumerable interface in .NET.
To iterate over an object using the for ... in syntax.
The array in ReoScript can be enumerated over every elements. (doesn't like JavaScript)
var arr = ['a', 'b', 10, 30];
// iterate array
for (element in arr) {
console.log(element);
}
The result is:
a
b
10
30
Enumerate over an object to get all the names of property.
var obj = { a: 10, b: 20 };
for (key in obj) {
console.log(key + " : " + obj[key]);
}
The result is:
a : 10
b : 20
String can also be enumerated over every characters.
.NET type implements IEnumerable interface will be available to enumerate in script.
public class StringObject : ObjectValue, IEnumerable
{
public string String { get; set; }
public override IEnumerator GetEnumerator()
{
for (int i = 0; i < String.Length; i++)
{
yield return String[i];
}
}
}
Now StringObject supported to iterate using for...in syntax:
var a = 'welcome';
for(c in a) {
console.log(c);
}
The result is:
w
e
l
c
o
m
e