js中利用原型prototype的方式实现继承是最常见的继承模式,如果让a的实例继承b,原型prototype的继承方式如下:

function A(){}

function B(){}

A.prototype = new B();

A.prototype = A;(个人认为最好有)

var a = new A();

console.log(a instanceof B)   // true

a.contructor 为 A的值

为啥不用A.prototype = B.prototype的方式呢,

因为如果更改A.prototype的话,会更改B.protptype 就是污染了B.prototype,就不能称之为继承。

而通过A.prototype = new B();如果更改A.prototype的话,不更改B.prototype,安全有效。

相关文章:

  • 2022-02-03
  • 2021-10-25
  • 2022-01-01
  • 2022-12-23
  • 2021-10-29
  • 2019-01-17
  • 2022-12-23
猜你喜欢
  • 2021-05-28
  • 2021-12-31
  • 2022-02-07
  • 2022-12-23
  • 2022-02-26
相关资源
相似解决方案