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