This article does not refer to the relevant content of ES6
In JavaScript we can use prototypes to implement inheritance.
For example:
function Baz () {
This.oo= "";
}
function foo () {
}
Foo.prototype=new Baz ();
var myfoo=new foo ();
myfoo.oo;
This allows us to access the attribute oo in Baz. In the actual use of this kind of drop, due to the sharing characteristics of the prototype (data stored on the heap),
All instances use a prototype, but a Baz attribute has a reference type that is tragic, and one instance modifies other instances to change ... Wuwuwu
Naturally, there is a combination of inheritance
function Baz () {
This.oo= "";
}
Baz.prototype.xx=function () {
}
function foo () {
Baz.call (this);// second call
}
Foo.prototype=new Baz ();// First Call
var myfoo=new foo ();
myfoo.oo;
myfoo.xx;
This will have a problem, the code also shows that the Baz will be called two times, as a Virgo how to allow it.
In the second way, is there a problem with the first way? The answer is no.
The reason is that the property lookup is first from the object itself, not found to go to the prototype to find, call when the attribute is inherited.
One more thing, so just use call inheritance. It's doable if you don't use prototypes, but how can you not use a prototype as a Virgo?
The method in the prototype is shared, so the performance is much better.
Parasitic combined inheritance
__extends=function (p,c) {
function ctor () {
this.constructor=c;//Assignment Constructors
}
Ctor.prototype=p.prototype;
C.prototype=new ctor ();
}
function Baz () {
THIS.OO=[1];
}
Baz.prototype.xx=function () {
}
__extends (Baz,foo);
function foo () {
Baz.call (this);
}
var myfoo=new foo ();
myfoo.oo;
myfoo.xx;
This not only solves the problem of two calls, but also solves the call of the object when the constructor is called the real function that created the object, not the other constructors on the prototype chain.
The code shows.
A constructor is a property on a prototype object and is the creator of the object. Since our prototype properties are newly assigned, the constructors are inherited.
Here's how the object is created, which is what new is doing.
For example:
var a=new b ();
In fact, a={}; created a to give a, then B.call (a); Call to initialize a, there is a step before call, is the internal prototype object of a
A prototype object that is set to the prototype property of B. There are constructor properties on the prototype that the constructor uses to create the object allocation memory control.
That's about it. Look at the time is not too early, break it, to maintain peace of mind do not impetuous, and strive to change tomorrow, hope that everything will slowly become better.
Inheritance in JavaScript