Symbol.iterator, .
, (entries, keys, values), , , , . , . , for..of. , [Symbol.iterator].
, Symbol.iterator, .
const iterator = obj[Symbol.iterator]();
for..of.
- ?
:
@@iterator "Symbol.iterator"
, Iterator . for-of.
( ) , :
const obj = {
[Symbol.iterator]: function () {
console.log('The iterator-returning function got invoked');
return 'abc'[Symbol.iterator]();
},
myMethod: function () {
return this[Symbol.iterator]();
},
myOtherMethod: function () {
return 'def'[Symbol.iterator]();
}
}
for (const a of obj) {
console.log(a);
}
for (const a of obj.myMethod()) {
console.log(a);
}
for (const a of obj.myOtherMethod()) {
console.log(a);
}
.as-console-wrapper { max-height: 100% !important; top: 0; }