@@iterator
method is part of
The iterable protocol
, that defines how to synchronously iterate over a sequence of values.
The initial value of the
@@iterator
property is the same function object as the initial value of the
values()
特性。
arr[Symbol.iterator]()
The initial value given by the
values()
iterator
. By default, using
arr[Symbol.iterator]
will return the
values()
函数。
<ul id="letterResult"> </ul>
var arr = ['a', 'b', 'c'];
var eArr = arr[Symbol.iterator]();
var letterResult = document.getElementById('letterResult');
// your browser must support for..of loop
// and let-scoped variables in for loops
// const and var could also be used
for (let letter of eArr) {
letterResult.innerHTML += "<li>" + letter + "</li>";
}
var arr = ['a', 'b', 'c', 'd', 'e']; var eArr = arr[Symbol.iterator](); console.log(eArr.next().value); // a console.log(eArr.next().value); // b console.log(eArr.next().value); // c console.log(eArr.next().value); // d console.log(eArr.next().value); // e
The use case for this syntax over using the dot notation (
Array.prototype.values()
) is in a case where you don't know what object is going to be ahead of time. If you have a function that takes an iterator and then iterate over the value, but don't know if that Object is going to have a [Iterable].prototype.values method. This could be a built-in object like
String
object or a custom object.
function logIterable(it) {
if (!(Symbol.iterator in Object.getPrototypeOf(it)
/* or "Symbol.iterator in Object.__proto__"
or "it[Symbol.iterator]" */)) {
console.log(it, ' is not an iterable object...');
return;
}
var iterator = it[Symbol.iterator]();
// your browser must support for..of loop
// and let-scoped variables in for loops
// const and var could also be used
for (let letter of iterator) {
console.log(letter);
}
}
// Array
logIterable(['a', 'b', 'c']);
// a
// b
// c
// string
logIterable('abc');
// a
// b
// c
logIterable(123);
// 123 " is not an iterable object..."
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Array.prototype[@@iterator]()' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@iterator
|
Chrome 38 | Edge 12 |
Firefox
36
|
IE No | Opera 25 | Safari 10 | WebView Android 38 | Chrome Android 38 |
Firefox Android
36
|
Opera Android 25 | Safari iOS 10 | Samsung Internet Android 3.0 | nodejs 0.12 |
完整支持
不支持
见实现注意事项。
使用非标名称。
Array.prototype.keys()
Array.prototype.entries()
Array.prototype.forEach()
Array.prototype.every()
Array.prototype.some()
Array.prototype.values()
Array
Array.from()
Array.isArray()
Array.of()
Array.prototype.concat()
Array.prototype.copyWithin()
Array.prototype.entries()
Array.prototype.every()
Array.prototype.fill()
Array.prototype.filter()
Array.prototype.find()
Array.prototype.findIndex()
Array.prototype.flat()
Array.prototype.flatMap()
Array.prototype.forEach()
Array.prototype.includes()
Array.prototype.indexOf()
Array.prototype.join()
Array.prototype.keys()
Array.prototype.lastIndexOf()
Array.prototype.map()
Array.prototype.pop()
Array.prototype.push()
Array.prototype.reduce()
Array.prototype.reduceRight()
Array.prototype.reverse()
Array.prototype.shift()
Array.prototype.slice()
Array.prototype.some()
Array.prototype.sort()
Array.prototype.splice()
Array.prototype.toLocaleString()
Array.prototype.toSource()
Array.prototype.toString()
Array.prototype.unshift()
Array.prototype.values()
Array.prototype[@@iterator]()
get Array[@@species]
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()