Generally, constructors do not return values. They use the keyword "this" to initialize objects without returning values. Of course, a constructor can return an object. If this is the case, the returned object will be the value of the new expression. In this case, the object with the value of this will be discarded /*
* (REFER P151)
* @ Time 2008-11-25
*/
The Code is as follows:
// No Return Value
Function Test0 (){
This. name = 'test0 ';
}
Var test0 = new Test0;
// Debugger;
Alert (test0); // output [Object]
Alert (test0.name); // output test0
// Return a String object
Function Test (){
This. name = 'test ';
Return new String ('20140901'); // returns a String object.
}
Var test = new Test ();
Alert (test); // output 123
Alert (test. name); // output undefined, indicating that the object created by a constructor is a string object.
// Return a string of the original type
Function Test2 (){
This. name = 'test2 ';
Return '20140901'; // returns a String object.
}
Var test2 = new Test2 ();
Alert (test2); // output [Object]
Alert (test2.name); // output test0