There is no concept of a class in JavaScript, there is only one constructor to create the object.
But JavaScript can also implement inheritance.
The first thing to say is that the objects in JavaScript are divided into function objects and normal objects.
What is a function object??
This is the object.
function f ()
{
}
In addition to the function object, all other objects are normal objects.
Let's talk about the relationship between constructors, instances, and prototype objects.
A prototype property inside a function object is used to refer to the object's prototype object. In fact, the prototype object is also very common.
The equivalent of implicitly writing code like this.
var fprototype = new F ();
F.prototype = Fprototype;
There is not much difference between a prototype object and a generic instance.
In general, the instance generated by the constructor has such a property---> __proto__ This property refers to its own prototype object.
Use a picture to illustrate the relationship between the three.
Next we're going to look at a break code.
Here, the author clearly does not define the Username property in the constructor but the result is the value.
Here it is, now look inside the current object to see if it has the current property name, if not the prototype object to find the attribute. Always follow the prototype chain to find Object.prototype.
If it is not found, it is undefined.
By applying this principle we can implement JavaScript inheritance.
We can draw a rough picture of this inheritance process.
Therefore, the key to implement the prototype chain inheritance is __proto__ rather than the prototype attribute in the function object.
Demo of code on prototype inheritance
function Parent (username)
{
this.username = "AAA";
}
Parent.prototype.sayHello = function ()
{
console.log (this.username);
};
PARENT.PROTOTYPE.AAA = function ()
{
console.log ("HelloWorld");
function child (username, password)
{
//Parent.call (this, username);
This.password = password;
}
Child.prototype.sayWorld = function ()
{
console.log (This.password);
};
Child.prototype = new Parent ();
var child = new Child ("Zhangsan", "123");
Child.sayhello ();
Console.log (child.__proto__.__proto__.__proto__.__proto__);
If there is a wrong place to speak, welcome to treatise, and hope to help you readers.