Maybe many people disagree with this. Adding the new Keyword before the function doesn't mean instantiating an object? But it is obviously not that simple: functionTest () {this. name & amp; #39; Test & amp; #39; returnfunction () {returntrue ;}} var... syntax
Maybe many people disagree with this. Adding the new Keyword before the function doesn't mean instantiating an object? But things are obviously not that simple:
Function Test (){
This. name = 'test ';
Return function () {return true ;}
}
Var test = new Test (); // What is test here?
Is it a Test object? Error! Here, test is a function -- function () {return true;} returned by Test ;}. In this case, new Test () is equivalent to Test (). Note that it is equivalent to, not equal to, if new Test () = Test () is used to determine whether the two are equal, false is returned because Javascript compares objects with functions based on references.
To better distinguish the differences between the two in the above circumstances, read the following code:
Function Test (){
This. name = 'test ';
Return 'test ';
}
Var fnT = Test ();
Var newT = new Test ();
Apparently, fnT is the string Test. What about newT? Are you confused by the first example? In fact, at this time, newT is a Test object -- there is an attribute named name whose value is the string Test.
Through the above two pieces of code, we can draw a guess that if the function return value is of the general value type (Number, String, Boolean, the new Function returns an Instance Object of the Function. If the Function returns a reference type (Object, Array, and Function), the new Function returns the same result as the Function called directly. This can be confirmed by returning different types of values in the Test function for testing.
It is actually quite important to clarify this point. At least there will be less doubt when looking at some object-oriented framework class library code.
From orain's column