The Return Value of the constructor in javascript and the process of the new object

Source: Internet
Author: User

The Return Value of the constructor in javascript and the process of the new object

First, it is clear that constructors in javascript do not need to return values, which is similar to java. We can think that the biggest difference between a constructor and a common function is that there is no return statement in the constructor, and a common function can have a return statement. In the constructor, this keyword is used to define the member variables and member methods, common functions do not use the this keyword to define member variables and methods.

Function Person (name, sex) {this. name = name; this. sex = sex; // return 1; // return true; // return "aty"; return {"returnValue": 11 };} // The new Keyword is considered as the creation object var aObj = new Person ("11", 1); alert (aObj. returnValue); // 11 alert (aObj. name); // undefined


The return value that the constructor does not need to display. When you use new to create an object (call the constructor), if the return value is not an object (such as a number, string, or boolean type), the return value is omitted. If the return value is an object, returns the object.

Next, we will briefly introduce the new object process in javascript: var myObj = newPerson ("aty", 25 );

1. Create an empty Object. var obj = new Object ();

2. Point this in the constructor Person to the newly created obj object.

3. Point the created obj _ proto _ to the prototype of the constructor Person. This step establishes a direct correspondence between the object and the prototype. Use

The _ proto _ attribute of the object can access the prototype, but no corresponding attribute is exposed in IE.

4. Execute the code in the constructor Person ().

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.