I learned that JS knows how to create objects.
var obj=new Object ();
var obj=new Function ();
Constructing objects with built-in function objects
You can also customize functions like this
function Test () {}
var baby=new test ();
Then there was a question
Console.log (typeof test);//function
The return is a function type
Console.log (typeof baby);//object
Why are you pinching like this?
That's what new is doing, baby is the object type, and that doesn't mean that it creates an object and returns
Let's add some seasoning to the test function to continue the validation.
function test (name) {
This.name=name;
This.say=function () {
Return this.name+ "Blow the best to say you are the most handsome"!
}
}
var baby=new test ("XJZ");
Console.log (baby.name);//XJZ
Console.log (Baby.say ());//xjz blow up and say he's the most handsome!
properties and methods are added to the automatically created object.
So far, new has done three things.
1.var obj={};//Creating an Object
2.this.name=name;this.say=funcion () {return this.name+ "blow the wind and say you're the most handsome!"};
This is the window object in the normal function
So we have to change this point to add properties and methods to obj Test.call (obj); The This in the test function changes to obj.
So you this.name=name is not obj.name=name; That's clear!!
3 Assigning the address of obj to the variable to the left of the equation
The next step is to need knowledge of the prototype chain, and I follow up on the prototype chain.
The 1.function function automatically adds a prototype prototype object (with only one copy of the properties and methods above him and all instances shared)
2.object objects have built-in prototype objects __proto__ Note is two _ (I have been here for a loss haha)
3. And obj.__proto__ the prototype object that points to its constructor
4.obj.__proto__ is a (address) prototype object that can find its constructor based on it
Obj.__proto__=itsconstructor.prototype;//itsconstructor (meaning of its constructors)
5. When there is no property or method you want in obj, it will look up at the first level of the prototype chain until you find the method you want or null to stop.
6 Here's the thing. If you don't know the prototype chain, it's going to be a little crazy.
Suppose Console.log (Baby.run ());
There is no run method in baby.
Then Baby.prototype will be found according to BABY.__PROTO__ (still not)
Because prototype is also the object (you know it typeof) so there are __proto__ properties
baby.prototype.__proto__-----> found Object.prototype (still not)
Because prototype is an object, of course, the Object.prototype is the ancestor of all objects.
Object.prototype.__proto__==null ends the prototype chain.
Let us add a method to the prototype object, if it can be used to explain the Baby.__proro__=test.prototype;
Test.prototype.look=function () {
Return this.name+ "Looking for beauty!";
}
Console.log (Baby.look ()); //XJZ is looking at the beauty of a successful visit to the
So, new, it's amazing that he did it.
Obj.__proto__=test.prototype;
So that sentence obj.__proto__ the concept of the prototype object that points to its constructor knows how .
Actually, the process of new is realized.
So in summary
New did four things.
1.var obj={};
2.obj.__proto__=test.prototype;
3.test.call (obj);
4 Assigning the address of obj to the variable to the left of the equation
I think the meaning of new is to save the code, the equivalent of the syntax sugar, and that it can hold all the properties and methods inside the constructor
And can also be extended.
I do not know whether the person who has seen can understand ... You have to look at the prototype and the prototype chain, or you won't understand.
This and scope can also be seen
What does JS new do and what does new mean?