<tfoot id='IIKDl'></tfoot>
    1. <legend id='IIKDl'><style id='IIKDl'><dir id='IIKDl'><q id='IIKDl'></q></dir></style></legend>

      <small id='IIKDl'></small><noframes id='IIKDl'>

    2. <i id='IIKDl'><tr id='IIKDl'><dt id='IIKDl'><q id='IIKDl'><span id='IIKDl'><b id='IIKDl'><form id='IIKDl'><ins id='IIKDl'></ins><ul id='IIKDl'></ul><sub id='IIKDl'></sub></form><legend id='IIKDl'></legend><bdo id='IIKDl'><pre id='IIKDl'><center id='IIKDl'></center></pre></bdo></b><th id='IIKDl'></th></span></q></dt></tr></i><div id='IIKDl'><tfoot id='IIKDl'></tfoot><dl id='IIKDl'><fieldset id='IIKDl'></fieldset></dl></div>

        <bdo id='IIKDl'></bdo><ul id='IIKDl'></ul>

        继承自 Set.prototype

        时间:2023-08-01
          <tbody id='48p0a'></tbody>
        <tfoot id='48p0a'></tfoot>

              <bdo id='48p0a'></bdo><ul id='48p0a'></ul>
                  <i id='48p0a'><tr id='48p0a'><dt id='48p0a'><q id='48p0a'><span id='48p0a'><b id='48p0a'><form id='48p0a'><ins id='48p0a'></ins><ul id='48p0a'></ul><sub id='48p0a'></sub></form><legend id='48p0a'></legend><bdo id='48p0a'><pre id='48p0a'><center id='48p0a'></center></pre></bdo></b><th id='48p0a'></th></span></q></dt></tr></i><div id='48p0a'><tfoot id='48p0a'></tfoot><dl id='48p0a'><fieldset id='48p0a'></fieldset></dl></div>
                  <legend id='48p0a'><style id='48p0a'><dir id='48p0a'><q id='48p0a'></q></dir></style></legend>
                1. <small id='48p0a'></small><noframes id='48p0a'>

                  本文介绍了继承自 Set.prototype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这真的让我很烦.我可以轻松地创建一个继承 Array.prototype 方法的新类:

                  var MyArray = function() {};MyArray.prototype = Array.prototype;var myArray = new MyArray();myArray.push(1);//这是允许的

                  同样的继承模式似乎不适用于 Set.prototype:

                  var MySet = function() {};MySet.prototype = Set.prototype;var mySet = new MySet();mySet.add(1);//TypeError: 方法 Set.prototype.add 在不兼容的接收器上调用

                  这是一个实施问题吗?是否有另一种可以工作的继承模式?我已经在节点 v0.12 和 Canary 中尝试过,结果相同.

                  这个解决方案有效,但我仍然不确定为什么上面的工作不一样:

                  var MySet = function(array) {var inst = new Set(array);inst.__proto__ = MySet.prototype;返回机构;}MySet.prototype = Object.create(Set.prototype);

                  解决方案

                  这是一个实施问题吗?是否有另一种继承模式可行?

                  不,按照规范,这种行为是正确的.Set 方法必须在实际集合(使用集合特定的内部槽初始化的对象)上调用,并且不像 Array 方法(基本上适用于所有具有.length 属性).

                  正如规范声明:p><块引用>

                  Set 构造函数被设计为可子类化.它可以用来作为 class 定义的 extends 子句中的值.子类打算继承指定 Set 行为的构造函数必须包括对 Set 构造函数的 super 调用以创建和用必要的内部状态初始化子类实例支持 Set.prototype 内置方法.

                  所以你将不得不使用 ES6 类语法来继承内置函数.

                  class MySet extends Set {}//默认构造函数有 super() 调用var mySet = new MySet();mySet.add(1);

                  是否支持这种子类化取决于实现,并非所有运行时和转译器都符合 ES6.

                  This is really bugging me. I can easily create a new class that inherits methods from Array.prototype:

                  var MyArray = function() {};
                  
                  MyArray.prototype = Array.prototype;
                  
                  var myArray = new MyArray();
                  myArray.push(1); // this is allowed
                  

                  The same inheritance pattern doesn't seem to work with Set.prototype:

                  var MySet = function() {};
                  
                  MySet.prototype = Set.prototype;
                  
                  var mySet = new MySet();
                  mySet.add(1); // TypeError: Method Set.prototype.add called on incompatible receiver
                  

                  Is this an implementation issue? Is there another inheritance pattern that will work? I've tried this in node v0.12 and Canary with the same results.

                  EDIT: This solution works, but I'm still not sure why the above doesn't work the same:

                  var MySet = function(array) {
                    var inst = new Set(array);
                    inst.__proto__ = MySet.prototype;
                    return inst;
                  }
                  
                  MySet.prototype = Object.create(Set.prototype);
                  

                  解决方案

                  Is this an implementation issue? Is there another inheritance pattern that will work?

                  No, this behaviour is correct as by the spec. The Set methods must be invoked on actual sets (objects initialised with set-specific internal slots), and are not generic as the Array methods (which basically work on everything that has a .length property).

                  As the spec states:

                  The Set constructor is designed to be subclassable. It may be used as the value in an extends clause of a class definition. Subclass constructors that intend to inherit the specified Set behaviour must include a super call to the Set constructor to create and initialize the subclass instance with the internal state necessary to support the Set.prototype built-in methods.

                  So you will have to use ES6 class syntax to inherit from the built-ins.

                  class MySet extends Set {} // default constructor has super() call
                  
                  var mySet = new MySet();
                  mySet.add(1);
                  

                  Whether this subclassing is supported depends on the implementation, not all runtimes and transpilers are ES6-compliant yet.

                  这篇关于继承自 Set.prototype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:创建一个独特组合的数组 下一篇:ldapjs 身份验证(用户登录设置)

                  相关文章

                  <i id='OGQyw'><tr id='OGQyw'><dt id='OGQyw'><q id='OGQyw'><span id='OGQyw'><b id='OGQyw'><form id='OGQyw'><ins id='OGQyw'></ins><ul id='OGQyw'></ul><sub id='OGQyw'></sub></form><legend id='OGQyw'></legend><bdo id='OGQyw'><pre id='OGQyw'><center id='OGQyw'></center></pre></bdo></b><th id='OGQyw'></th></span></q></dt></tr></i><div id='OGQyw'><tfoot id='OGQyw'></tfoot><dl id='OGQyw'><fieldset id='OGQyw'></fieldset></dl></div>

                2. <small id='OGQyw'></small><noframes id='OGQyw'>

                      • <bdo id='OGQyw'></bdo><ul id='OGQyw'></ul>
                    1. <tfoot id='OGQyw'></tfoot>
                      <legend id='OGQyw'><style id='OGQyw'><dir id='OGQyw'><q id='OGQyw'></q></dir></style></legend>