1 functionouter () {2 functioninner () {}3 returnInner;4 }5 6 varA=outer ();7 varb=outer ();8 varC=Newouter ();9 varD=NewOuter ();
Question: What are these values?
a==b;a.constructor= =B.constructor;b.constructor==c.constructor;
A.prototype.constructor;
B.prototype.constructor==c.prototype.constructor;
Answer: false; True; true; function inner () {}; false;
Parsing: The knowledge point is the return value of the function call, the instance object of the new constructor, the prototype, the Construcor constructor, and so on.
- var a=outer (); var b=outer (); The outer function is called and executed, and the outer function returns a reference to the inner function, which is stored in a B variable. The misconception is that I used to think that the same reference was returned to the same inner in memory as a, B. is actually two different references, although their content is the same, it can be understood thata saved reference points to a copy of the inner memory area, B save the reference to another copy of the inner memory area .
- Since the call to execute outer returns a reference to the function, we know that the constructor property of all functions points to the total constructor of function function () {[native code]}, so a.constructor== B.constructor, pointing to the same function, of course, is true.
-
var c=new outer (); var d=new outer (); It appears that the instance object is returned in the form of the new constructor, while executing the constructor outer executes a return statement, which seems rare, and the common return value is the instantiated object of this constructor. But JS Dafa is awesome, JS's constructor can have a return value can also not (in three cases: no return value is returned to the instantiated object, if the return value checks whether the return value has a reference type, if it is a non-reference type such as the basic type is ignored, or return its instantiation object, if the return value is a reference type, such as the return {} object, such as the return fun function, the actual return value is the reference type. (Reference from: http://www.cnblogs.com/xxcanghai/p/5189353.html#3361101). Here C,d all save the returned reference, point to their own inner function object, so C.constructor also point to function, B.constructor==c.constructor; true.
- A is now pointing to a block in the memory area, the type is a function, and the function content is the same as the inner function, since it is a function that has its own prototype prototype attribute, The A.prototype prototype property also has the constructor property, which also points to the function that has prototype, which is the function where a is directed.
- b in the same vein, b.prototype.constructor points to a function that points to B, and c.prototype.constructor;c itself points to a function, so so too c.prototype.constructor points to the function that C points to.
Although it looks like constructor looks the same, in fact, is not the same!
A note about scopes: A,b,c,d has its own scope chain and then extends to the global environment.
JavaScript Constructor Small note