<HTML>
<body>
</body>
<script>
let range = {
from: 1,
to: 5,
[Symbol.iterator]() {
this.current = this.from;
return this;
},
next() {
if (this.current <= this.to) {
return { done: false, value: this.current++ };
} else {
return { done: true };
}
}
};
let arr = Array.from(range, num => num * num);
//second argument arrows each member of the object
alert(arr);
</script>
</HTML>
//itearble means an object with Symbol.iterator and next()
//so that it can be used for for - of and array.from
//array-like is an object, which have an index for each item
//and have an member length
//string has both traits of arraylike and iterable,
//but normally both trait cannot stand together in one object
<body>
</body>
<script>
let range = {
from: 1,
to: 5,
[Symbol.iterator]() {
this.current = this.from;
return this;
},
next() {
if (this.current <= this.to) {
return { done: false, value: this.current++ };
} else {
return { done: true };
}
}
};
let arr = Array.from(range, num => num * num);
//second argument arrows each member of the object
alert(arr);
</script>
</HTML>
//itearble means an object with Symbol.iterator and next()
//so that it can be used for for - of and array.from
//array-like is an object, which have an index for each item
//and have an member length
//string has both traits of arraylike and iterable,
//but normally both trait cannot stand together in one object
댓글
댓글 쓰기