Now to their own understanding of things, summed up, to organize to this, look at my that will draw a picture, I do not know what to say, that will be the moment to dredge the feeling, now basically blurred, I this brain is really ...
Get started:
One: Think about what new has done, and here's an example
function A () { var a1=111; this. a2=222; function fa () { console.log (A1); } FA (); } a.prototype.a3= "333"; var New A ();
New A constructor,
1: A new object will be created first, such as Var o={};
2: Then o the prototype of the __PROTO__=A function of the object,
3: Execute a,a.apply with O for execution Environment (O,[arg])
What is apply, which is execution, changes the execution context and turns the this in the a execution environment to O,
See a lot of places to say, the difference between apply and call, I think the difference is that they are two different, apply is an array form: Apply (obj,argument) argment is an array, call is directly the value of the form: Call (Obj,name,age)
4: Look at the case of a function return value, and then B is what, for the previous example,B points to {a2:222} , inside the __proto__, constructor and so on will say
The yellow part of the article does not understand, supposedly he is the system generated O object, but why he is a{}?
Now look at the various cases of the A method return value and see what B is
1: What does not return the situation
2: Case of Return data type
3: Case of return reference type
Summary, when new a function, if the function has a return, if the data type, directly ignore, return the system-generated object, if the return of complex data type, then is the complex data type, if not returned, is the system-generated object
Two prototype
Each function has a prototype (prototype) attribute whose value is an object (property collection)
There is only one constructor property, which points to itself, since his value is an object, including the constructor and __proto__ properties, and you can customize the properties, for example:
Three: __proto__
__proto__ is an invisible property, and each object has this property, and the __proto__ value of the instantiated object is a pointer to that object prototype
Four: Custom Function objects object function The connection between them prototype __proto__
Note the __proto__ in the prototype property collection of the custom function B, or the entire prototype is the object, his __proto__, pointing to the Bottommost object, if this new condition is
var a = function () {this.a1= "a";};
var B = function () {this.b1= "B";};
var c = function () {this.c1= "C";};
B.prototype = new A ();
C.prototype = new B ();
B.PROTOTYPE={A1: "A"};
B.prototype.__proto__=a.prototype, the value of A.prototype is constructor and __proto__ (pointing to the bottommost object{}),
C.PROTOTYPE={B1: "B"};
c.prototype.__proto__=b.prototype--"point to {A1:" A "};
I don't know if I understand the wrong place ah ah ah ah ...
What JS New has done prototype __proto__