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 ().