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