search

Home  >  Q&A  >  body text

javascript - About Object.create() creation inheritance

The following is the code

function A(){
  this.a = 1;
  this.b = 2;
}
A.prototype.drive = function(){
  console.log('drivvvvvvvvvv');
}
//方式1
function B(){}
B.prototype = Object.create(new A()); //这里采用了new 一个实例
//方式2
function C(){
  A.call(this);
}
C.prototype = Object.create(A.prototype) //这里使用的是父类的原型

What is the difference between the above two methods? Why do I think mdn uses the second one?

滿天的星座滿天的星座2796 days ago573

reply all(2)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-19 10:17:44

    1 Disadvantages:

    1. Executing new is equivalent to running A once. If you do some other things in A (such as changing global variables), there will be side effects.

    2. Use the object created by A as a prototype, which may have some redundant attributes.

    2 simulates the execution process of new

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-19 10:17:44

    Longer

    reply
    0
  • Cancelreply