javascript - About Object.create() creation inheritance
滿天的星座
滿天的星座 2017-05-19 10:15:44
0
2
511

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?

滿天的星座
滿天的星座

reply all(2)
过去多啦不再A梦

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

Peter_Zhu

Longer

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!