JavaScript: Similarities and Differences between a new function and a function called directly

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.