Fully understand the inheritance in JavaScript (mandatory). javascript is mandatory.
In JavaScript, we can use prototype to implement inheritance.
For example
function baz(){this.oo="";}function foo(){}foo.prototype=new baz();var myFoo=new foo();myFoo.oo;
In this way, we can access the attributes oo in baz. In actual use, this is not feasible because of the sharing characteristics of the prototype (data is stored on the stack ),
All instances use a prototype. Once the baz attribute has a reference type, it is tragic. Once an instance is modified, other instances are changed.
Naturally, combined inheritance is available.
Function baz () {this. oo = "";} baz. prototype. xx = function () {} function foo () {baz. call (this); // second call} foo. prototype = new baz (); // The first call var myFoo = new foo (); myFoo. oo; myFoo. xx;
In this way, there will be a problem. The Code also shows that baz will be called twice. How can this problem be permitted as a Virgo ..
Do you need to skip the first method in the second method? The answer is no.
The reason is that the property search starts from the object itself, and will go to the prototype before it is found, and the property will be inherited during the call.
Just insert a sentence. Will it be okay to only use the call inheritance function? If prototype is not used, it is feasible, but as a Virgo, how can we not use prototype,
The prototype method is shared, so the performance is much better.
Parasitic combined inheritance
_ Extends = function (p, c) {function ctor () {this. constructor = c; // value assignment constructor} 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 problem that when an object calls a constructor, it actually creates an object function rather than other constructor functions on the prototype chain.
The code indicates.
Constructor is an attribute of the prototype object and the creator of the object. Because our prototype attributes are assigned a new value, they are inherited from the constructor.
Here we will talk about how to create an object, that is, what new has done.
For example:
Var a = new B ();
In fact, a = {}; creates a file for a, and then B. call (a); initialize a in call. There is another step before call, that is, the internal prototype object of.
The prototype object to which the prototype property is set to B. The prototype has the constructor attribute, which is used to create an object allocation memory control.
Maybe that's all... it's not too early to look at it. Let's break it down. Keep a peaceful mind and never be impetuous. Try to change tomorrow and hope everything will get better.
The above is all the content of the Inheritance (mandatory) in JavaScript provided by the editor. I hope you can support it a lot.