To achieve higher data code sharing, you should use prototype mode.
1. Prototype objects
In JavaScript, when we create a function, we have a prototype property, which is the prototype property. This property is a pointer to an object that is used to implement the properties and method sharing of the instance.
By default, all prototype objects automatically get a constructor property that is the constructor property, which points to the original object.
This god horse memory points to the figure I will not draw. So Visio can draw this kind of picture wow. There is a great God to explain the wood ~
Anyway it is:
Dog.prototype.constructor==dog;//true
Dog1.prototype.constructor==dog;//true
You can also use:
Dog.prototype.isPrototypeOf (DOG1);//trueobject.getprototypeof (DOG1) ==person.prototype;//true
Although the values saved in the prototype can be accessed through an object instance, the values in the prototype cannot be overridden by an instance, and if we add a property of the same name to the instance, it can be understood to overwrite with the same name (which opens up memory).
2. prototype function
function Dog () {}dog.prototype.age=11;dog.prototype.size=22;dog.prototype.toage=function () { alert (this.age);}; var dog1=new dog (), Var dog2=new dog ();d og1.age=20;dog1.toage (),//20, from Instance dog2.toage ();//11, from prototype
The delete operator can delete instance properties, allowing us to revisit the properties in the prototype.
Again, the hasOwnProperty () method can be accessed only after the instance overrides the property.
When the in operator is used alone, it can be accessed regardless of whether the attribute exists in the instance or in the prototype.
In addition, the Hasprototypeproperty () function is used to access properties in the prototype.
To get all the enumerable instance properties on an object, you can use the Object.keys () method. For example:
function Dog () {}dog.prototype.age=11;dog.prototype.size=33;dog.prototype.toage () { alert (this.age);}; var keys=object.keys (Dog.prototype); alert (keys);//"Age,size,toage" var p1=new Dog ();p 1.age=10;p1.size=44;var P1keys =object.keys (p1); alert (p1keys);//"Age,size"
If you want to get all the instance properties you can use the Object.getownpropertynames () method. Use the following:
var keys=object.getownpropertynames (Dog.prototype); alert (keys);//"Constructor,age,size,toage"
Here's how to get properties. All out of the side, all out of the back, this part of me to blow a big, everybody look on the line ...
In fact, the simpler prototype syntax is as follows:
function Dog () {}dog.prototype={ age:12, size:44, toage:function () { alert (this.age); }}; var dog=new dog;
But here in fact, the prototype object is completely rewritten, so the constructor property of the prototype object at this point does not point to the dog function. For example:
Alert (dog instanceof Object);//truealert (dog instanceof Dog);//truealert (Dog.constructor==object);//truealert ( Dog.constructor==dog);//false
Although using instanceof also returns the correct result, constructor has been unable to determine the type of the object. However, the constructor can still be set to dog. For example, write this:
dog.prototype={ Constructor:dog};
Note that the reset constructor is only available for ECMASCRIPT5 compatible browsers. And resetting the constructor will make the default constructor enumerable, and you can use the Object.defineproterty () function to change.
Object.defineproterty (Dog.prototype, "constructor", { enumerable:false;//set to non-enumerable Value:dog});
After overriding the prototype object, the property is inaccessible after overriding the previous instance.
Understand carefully: pointers in an instance point only to prototypes, not to constructors. When the prototype is rewritten, the connection between the function and the initial prototype is severed. The instance still points to the initial prototype, and the instance cannot access the properties of the overridden prototype.
I have a question, there is a big God to help answer the wood, that is, I can access the prototype of the instance. Why I am using dog1.prototype output undefined.
By the way, the native object method is also defined in the prototype, such as (object,array,string, etc.).
Javascrip Prototype schema Creation object