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

  • <small id='ZFZcQ'></small><noframes id='ZFZcQ'>

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

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

        在 JavaScript 类中使用箭头函数的继承和多态

        时间:2024-04-18
        • <bdo id='927Xr'></bdo><ul id='927Xr'></ul>
              <tfoot id='927Xr'></tfoot>
            1. <small id='927Xr'></small><noframes id='927Xr'>

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

                  本文介绍了在 JavaScript 类中使用箭头函数的继承和多态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  为什么在 JavaScript 类中箭头函数优先于函数声明?

                  Why do arrow functions take precedence over function declarations in JavaScript Classes?

                  例子:

                  
                  class Parent {
                  
                      work = () => {
                          console.log('This is work() on the Parent class');
                      }
                  }
                  
                  class Child extends Parent {
                  
                      work() {
                          console.log("This is work() on the Child class ");
                      }
                  
                  }
                  
                  const kid = new Child();
                  
                  kid.work();
                  

                  在这个例子中触发了父 work() 方法:

                  The parent work() method fires in this example :

                  这是父类上的 work()"

                  我只是想了解为什么箭头函数在 JS 类中总是优先,尤其是在继承和多态方面.

                  I just want to understand WHY the arrow function always takes precedence in JS Classes, especially in regards to Inheritance and Polymorphism.

                  推荐答案

                  跟做箭头函数没关系.它优先,因为它是 类字段.类字段被添加为实例的 own 属性,而方法被添加到 Child.prototype.work.即使你把它从箭头函数改成普通函数,它仍然会执行类字段.

                  It is nothing to do with being an arrow function. It is taking precedence because it's a class field. Class fields are added as an own property of the instance while methods are added to Child.prototype.work. Even if you change it from an arrow function to a regular function, it will still execute the class field.

                  当你访问kid.work时,查看属性的顺序是

                  When you access kid.work, the order in which the property will be looked is

                  • 自己的属性,直接在kid对象下(可以在这里找到)
                  • Child.prototype.work
                  • Parent.prototype.work
                  • 如果还没有找到,就会在Object.prototype
                  • 里面找
                  • own properties, directly under kid object (It is found here)
                  • Child.prototype.work
                  • Parent.prototype.work
                  • If still not found, it will be looked inside Object.prototype

                  class Parent {
                    // doesn't matter if it an arrow function or not
                    // prop = <something> is a class field
                    work = function() {
                      console.log('This is work() on the Parent class');
                    }
                  }
                  
                  class Child extends Parent {
                    // this goes on Child.prototype not on the instance of Child
                    work() {
                      console.log("This is work() on the Child class ");
                    }
                  }
                  
                  const kid = new Child();
                  
                  // true 
                  console.log( kid.hasOwnProperty("work") )
                  
                  // true
                  console.log( Child.prototype.hasOwnProperty("work") )
                  
                  // false 
                  // because work inside Parent is added to each instance
                  console.log( Parent.prototype.hasOwnProperty("work") )
                  
                  kid.work();
                  
                  // How to force the Child method
                  Child.prototype.work.call(kid)

                  这篇关于在 JavaScript 类中使用箭头函数的继承和多态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:javascript oop、instanceof 和基类 下一篇:为什么单态和多态在 JavaScript 中很重要?

                  相关文章

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

                  2. <tfoot id='evKUt'></tfoot>
                  3. <legend id='evKUt'><style id='evKUt'><dir id='evKUt'><q id='evKUt'></q></dir></style></legend>