Before we learned about objects, we talked about how to create objects, one that creates a new object through a constructor, and one that creates objects in the form of object literals.
It is important to understand the concept of a prototype object when it comes to objects.
1 concept of prototype objects
The
constructor has a default property, prototype property, which points to an object that is the prototype object of the constructor.
The
also has a constructor attribute on the prototype object, which points to the constructor itself.
2 features in a prototype object
The properties and methods in the
Prototype object can be shared by Instance objects (the properties and methods in the prototype function called by different instance objects are the same).
3 instance object points to prototype object
The instance object created by the constructor has a __proto__ property that points to the prototype object of the constructor.
However, because the __proto__ property is nonstandard, it is not recommended to use the __proto__ property to find the prototype function. If you need to set properties on a prototype object, we recommend that you use the prototype property of the constructor to locate the prototype object.
4 instance calls the property or method's procedure
When the instance. Property name is called, the instance first looks for the property in itself, and if it does not have this property, it finds the prototype object, finds it in the prototype object, and if it has not yet found the prototype object on the prototype, until the top of the prototype chain (that is, no longer have the prototype object).
5 Object Structure map
The
Prototype object is more abstract, and we can only deepen the understanding through the structure diagram.
Case code:
function Animal (name,color) {
This.name=name;
This.color=color;
}
Animal.prototype.cry=function () {
Console.log ("I Am" +this.name, "I was bullied, 55555");
}
var cat=new Animal ("Tony", "gray-white");
var dog=new Animal ("Ha II", "Brown");
The corresponding structure diagram:
Basic concepts for JavaScript-based objects