JS New The difference between a function and a direct call function

Source: Internet
Author: User



The difference between using new and calling a function: If the function return value is a value type (number, String, Boolen), the new function returns an instance object of the function, and if the return value of the function is a reference type (object, Array, Funtion), the new function is the same as the result of the direct call function. The test code is as follows:
<script type= "Text/javascript" >/*//Factory mode function person (name,age) {var o = new Object ();        O.name = name;        O.age = age;        O.getname = function () {alert (this.name);    } return o;    } var obj1 = new Person ("Liwen", 25);    var obj2 = person ("Liwen1", 25);  Obj1.getname ();  liwen*/new instance object of a function obj2.getname (); liwen1*/Direct Call

The object of the new function here is the same as the result of the direct call function, and the Name property of the function can be ejected. Note that the return value of the function here is a Funtion object
/* If a return statement exists in a function, is it the same as the result of calling this function using new and not using new?        */function Test () {this.name = ' test ';        return function () {return true;   }}//var Test = new test ();      function () {return true;}       It is an object//var test = Test ();      function () {return true;}   It is function//alert (new test () = = Test ()); False This is strange, both are clearly the same, are functions, how different, because JS for object and funtion comparison is based on the reference.
In order to further distinguish between the two differences in the above situation, continue to see the following code function test () {this.name = "test";    return "Test";   } var test1 = new Test ();    Object, which has a Name property, and returns a string of test var test2 = Test (); Test, which is simply a string
/* With the above code, you can get a guess: if the function return value is a numeric type (number, String, Boolean) in the general sense, the new function returns an instance object of the function, and if the function returns a reference type (object, Array, Functions), the new function produces the same result as a direct call to a function */
</script> on the Web: the difference between calling a function with new: If the function return value is a value type (number, String, Boolen), the new function returns the instance object of the function. If the return value of this function is a reference type (Object, Array, Funtion), the new function is the same as the result of the direct call function. The test code is as follows:
<script type= "Text/javascript" >/*//Factory mode function person (name,age) {var o = new Object ();        O.name = name;        O.age = age;        O.getname = function () {alert (this.name);    } return o;    } var obj1 = new Person ("Liwen", 25);    var obj2 = person ("Liwen1", 25);  Obj1.getname ();  liwen*/new instance object of a function obj2.getname (); liwen1*/Direct Call

The object of the new function here is the same as the result of the direct call function, and the Name property of the function can be ejected. Note that the return value of the function here is a Funtion object
/* If a return statement exists in a function, is it the same as the result of calling this function using new and not using new?        */function Test () {this.name = ' test ';        return function () {return true;   }}//var Test = new test ();      function () {return true;}       It is an object//var test = Test ();      function () {return true;}   It is function//alert (new test () = = Test ()); False This is strange, both are clearly the same, are functions, how different, because JS for object and funtion comparison is based on the reference.
In order to further distinguish between the two differences in the above situation, continue to see the following code function test () {this.name = "test";    return "Test";   } var test1 = new Test ();    Object, which has a Name property, and returns a string of test var test2 = Test (); Test, which is simply a string
/* With the above code, you can get a guess: if the function return value is a numeric type (number, String, Boolean) in the general sense, the new function returns an instance object of the function, and if the function returns a reference type (object, Array, Functions), the new function produces the same result as a direct call to a function */
</script>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JS New The difference between a function and a direct call function

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.