The return value of the JavaScript function
- The function returns all undefined without an explicit return value
- When the function has a definite return value:
- If the function return value is a value type (number, Boolean, String) in the general sense, the new operator returns an instance object of the function;
- If the function returns a reference type (Object, Array, function), the result of the direct call function is equated with the new operator;
The return value type of a function is a value type (number, Boolean, String):
1 function person () { 2 var name = ' Tom ' ; 3 return ' Jack ' ; 4 5 6 var p = new person (); // 7 8 person (); //
Similarly, change the way you declare a variable from Var to this, as follows:
1 function Person () {2 this. Name = ' Tom '; 3 return ' Jack '; 4 }56varnew person (); // P person{name: ' Jack '} similarly 7 person (); // "Jack"
Return a worthwhile type is a reference type (Object, Array, Function):
1 functionPerson () {2 This. Name = ' Tom ';3 return{name: ' Jack '};4 }5 6 varp =NewPerson ();//P object{name: ' Jack '};7P.name;//' Jack '8Person ();//object{name: ' Jack '}9 Ten //thus, when the return value type of a function is a reference type, the new operator is equivalent to the result of a direct call;
JavaScript functions call regular calls and are called with new