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: Assuming that the function return value is a value type (number, String, Boolen), the new function returns the instance object of the function. Assuming that the return value of this function is a reference type (Object, Array, Funtion), the new function is the same as the result of calling the function directly. Test code such as the following:
<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 and the result of the direct call function will also be able to eject the function's Name property. Note that the return value of the function here is a Funtion object
/* Suppose there is a return statement 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 the functions, how not the same, because JS for object and Funtion is based on the comparison of reference.
In order to further distinguish between the two differences in the above situation, continue to look at 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
/* Through the above code, you can get a push: Assuming that 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, assuming that the function returns a reference type (object, Array, Functions), the new function produces the same results as the direct call function.
</script> Online: The difference between calling a function with new: Assuming that the function return value is a value type (number, String, Boolen), the new function returns the instance object of the function. Assuming that the return value of this function is a reference type (Object, Array, Funtion), the new function is the same as the result of calling the function directly. Test code such as the following:
<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 and the result of the direct call function will also be able to eject the function's Name property.

Note that the return value of the function here is a Funtion object


/* Suppose there is a return statement 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 all functions, how not the same. Since JS is based on reference to the comparison of object and funtion.
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. It's simply a string
/* Through the above code, you can get a push: Assuming that 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, assuming that the function returns a reference type (object, Array, Functions), the new function produces the same results as the direct call function.
</script>

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.