Consider: There is an object that you want to reuse, and the second object you want to create needs to get its functionality from the first pair of objects.
The implementation is as follows:
// the object to inherit var parent = { name:"Papa"}; // New Object var child = object (parent); Console.log (child.name);
The object function is implemented as follows:
// prototype Inheritance function Object (o) { function F () {}; = o; return New F ();};
Discuss:
You can use constructors to create a parent object, so that the "self" property and the stereotype properties of the constructor are inherited.
// Parent Constructor function Person () { this. Name = "Jie";}; // attributes added to the prototype function () { returnthis. name;}; var New Person (); var kid = object (papa); // Test Console.log (Kid.getname ());
Another variation of this pattern is that you can choose to inherit only the prototype object of an existing constructor.
// Parent Constructor function Person () { this. Name = "Jie";} // attributes added to the prototype function () { returnthis. Name;} var kid = object (person.prototype); Console.log (typeof kid.name); Results "undefined" console.log (typeof Kid.getname); Result "function"
Summary of the prototype inheritance of JS