Suppose you have the following code:
function A () {this. Name = ' A '}function B () {this. Name = "B"function() { /* */};
To make B inherit a at this time, use
New= B;
I want to know why this is B.prototype = new A (); Instead of B.prototype = A or B.prototype = A.prototype?
There are also:
var New A (); var New B ();
Since c = = B.prototype = = new A (); What's the relationship between B and C?
Lee Citation
Links: http://www.zhihu.com/question/20801227/answer/16239270
Source: Know
1. Why is B.prototype = new A (); Instead of B.prototype = A or B.prototype = A.prototype?
To implement inheritance, you must ensure that B inherits from a, and that changes made by B do not affect a and other objects that inherit from a.
If B.prototype = A, then the reference to the two objects is exactly the same, so that if the assignment is b.prototype.name=45455, then the a.name becomes 45455 directly;
The direct change of a and other objects inherited from a is not inherited at all.
As for b.prototype = A.prototype, the same is true of modifying the prototype of B to directly pollute the prototype of a.
B.prototype = new A (); This method creates a new object {} and inherits the prototype of a, which is a new object, not the same reference as a, so it does not pollute a.
We first changed the B.prototype to another name, called XXX, at this time xxx=new a ();
when you request xxx. Whatever, it does not exist at this time and will be looked up from the constructor, i.e. a ().
That is B.prototype whatever ==xxx. Whatever ==a.prototype.whatever
This is a smooth request to the value, and does not pollute a.
B.prototype.constructor = B;这行是把B的constructor重新设为B,不然的话它就会变成A
2.var C = new A (); This line, it is a new inherited object.
Although B.prototype=new a () is defined above; But B.prototype and C are not equal, they are two completely different objects.
If you want them to be equal, you don't have to inherit ... Give it a direct assignment.
var c = new A();var b = new B();
This is the classic of the prototype chain.
I'd like to request a b.whatever.
So first it's looking in the B object, not found.
And then it goes to B's constructor B.prototype, and this time there's no
So it went to B.prototype's constructor A to find, finally found A.prototype.whatever, so it returned to B.prototype whatever, and then returned to B.whatever
This realizes the inheritance relationship of the prototype chain.
B.whatever==b.prototype.whatever==a.prototype.whatever
The purpose of this is that B completely inherits the thing of a, as long as there is a prototype, B's instance (generated by new B) can be obtained.
However, modifying the properties and methods of an instance does not affect the class (well, JS does not have these two words, meaning almost)
Modifying the properties and methods of a subclass does not affect the parent class (well, JS does not have these two words, meaning almost)
Different instances are different objects and are not equal.
3. The difference between the inheritance and the example is very good understanding, you above that, C is an example of a, B is a inheritance, they still have a very big difference.
B can be instantiated as a constructor, at which point the instance of B can get the properties and methods of the parent Class A.
The example of a C cannot be instantiated, i.e. it cannot be used xxx=new C ();
B's prototype completely copied a, that is, B.prototype and a prototype.
C There is no prototype, it is not any prototype.
JavaScript Inherits code, B.prototype = new A (); What does it mean? [Turn to know]