阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      JS Proxy enumerate()

      已废弃
      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

      被代理的目标对象。

      Return value

      该方法应当返回一个迭代器

      描述

      handler.enumerate 方法决定了被代理对象在for...in时的行为。

      触发条件

      这些操作可以触发这个方法。

      Invariants

      If the following invariants are violated, the proxy will throw a TypeError:

      • The enumerate method must return an object.

      Examples

      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].

      Specifications

      SpecificationStatusComment
      ECMAScript 2015 (6th Edition, ECMA-262)[[Enumerate]]StandardInitial definition. Removed in ECMAScript 2016.

      See also

      目录
      目录