What does Person return when instantiating Person (or what p equals)?
function Person(name) {
this.name = name
return name;
}
let p = new Person('Tom');
Copy the code
To be honest, at first I thought the value was ‘Tom’, but when I threw the code to the console and output it, I realized I was wrong. Oh my god, did you ignore the new operator? Regardless of the new, let’s change the code
function Person(name) {
this.name = name
return name;
}
let p = Person('Tom');
console.log(p);
Copy the code
Obviously, the output is ‘Tom’, but what about new? So let’s get this straight. First, I searched MDN for the definition of new
The new operator creates an instance of a user-defined object type or of a built-in object with a constructor.
Emmmm, it’s pretty obscure.
So let’s try to write down some chestnuts and see what happens
function Person1(name) {
this.name = name;
// No return value
}
function Person2(name) {
this.name = name;
return name;
// Returns a non-object
}
function Person3(name) {
this.name = name;
return { a: 1 };
// Return the object
}
function Person4(name) {
this.name = name;
return null;
/ / returns null
}
var p1 = new Person1("aa");
var p2 = new Person2("bb");
var p3 = new Person3("cc");
var p4 = new Person4("dd");
console.log(p1); // Person1 {name: "aa"}
console.log(p2); // Person2 {name: "bb"}
console.log(p3); // {a: 1}
console.log(p4); // Person4 {name: "dd"}
Copy the code
According to the above several chestnut, we can draw the conclusion: when using the new object to create | | calls the constructor, if a function has no return value | | the return value is object, then the return after the constructor instance object (note: return null, the returned after instance constructor objects rather than null). If the function returns an object, it returns that object
Let’s move on to the EXPLANATION of the MDN documentation. After all, these demos are not convincing.
Let’s take a look at what new does
function Person(name) {
this.name = name;
return {a: 1}}var p = new Person('fe')
Copy the code
When calling new Person(…) , the following steps will be taken:
- The first is inherited from
Person.prototype
The new object will be created - Using parameter
'fe'
Calling the constructorPerson
And willthis
Bind to the newly created object - by
Person
The object returned isnew
The result of the expression =Person
The object returned is{a: 1}
sonew
The result of this expression is zero{a:1}
; ifPerson
If there is no return value (constructors generally do not return a value) then the object created using Step 1, i.e. ==, inherits fromPerson.prototype
The new object
A simulated implementation of new is provided by the new operator. The simulated implementation of new is provided by the new operator
function objectFactory() {
var obj = new Object(),
cons = [].shift.call(arguments)
obj.__proto__ = cons.prototype
var ret = cons.apply(obj, arguments)
return typeof ret === 'object' ? ret|| obj : obj
}
function Person(name) {
this.name = name;
return {a: 1}}var p = objectFactory(Person, 'fe')
Copy the code
Of course, learning from someone else’s code is not just copying it, but at least understanding it. use
- The first step is to create an object
cons
Is to callobjectFactory
The first argument to the method, the constructor; becauseshift
Will change the original array, so the changedargument
(Arguments is an array object corresponding to the arguments passed to the function.)- will
obj
The prototype points to the constructor so that OBj can access the properties on the constructor prototype - Constructor function
cons
的this
Point to theobj
So that theobj
Can access properties in the constructor - Determine if the returned value is an object, and return it if it is
return null
Because of the legacy of historytypeof null === 'object'
); Returns if it is not an objectobj
(Note: hereobj
Is no longer an empty object.
If you’ve had the patience to see this, thank you very much. If the article has mistakes, hope to give correction ~