Javascript constructor Example Analysis _ javascript skills

Source: Internet
Author: User
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

Related Article

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.