已废弃
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.
代理方法handler.enumerate()
决定了被代理对象在for...in
中的行为。不过这个方法已经在ES2016标准中被移除了。
var p = new Proxy(target, {
enumerate(target) {
}
});
下列参数将会被用以调用 enumerate
方法。this将会指向处理器对象。
target
该方法应当返回一个迭代器对象。
handler.enumerate
方法决定了被代理对象在for...in
时的行为。
这些操作可以触发这个方法。
for (var name in proxy) {...}
Reflect.enumerate()
If the following invariants are violated, the proxy will throw a TypeError
:
enumerate
method must return an object.The following code traps for...in
statements.
var p = new Proxy({}, {
enumerate(target) {
console.log('called');
return ['a', 'b', 'c'][Symbol.iterator]();
}
});
for (var x in p) { // "called"
console.log(x); // "a"
} // "b"
// "c"
The following code violates the invariant.
var p = new Proxy({}, {
enumerate(target) {
return 1;
}
});
for (var x in p) {} // TypeError is thrown
Note: Both examples make use of the shorthand syntax for method definitions.
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) [[Enumerate]] |
Standard | Initial definition. Removed in ECMAScript 2016. |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
enumerate | Chrome No support No | Edge No support No | Firefox No support 37 — 47 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Edge Mobile No support No | Firefox Android No support 37 — 47 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
Proxy
handler
for...in
statementsReflect.enumerate()