Javascript: inheritance parsing and javascript Parsing
In the previous article, we have introduced the inheritance of combinations. Now we will talk about several inheritance types.
Original Type inheritance
Call a function to receive the objects returned by this function. The prototype of this object is the parameter object of the input function.
For example:
function personObject(o){ function F(){} F.prototype = o; return new F();}var person = { name:"Nicholas", friends:["Shelby","Court","Van"]}var person_one = personObject(person);
From the code above, we know that person is the prototype of person_one. ES5 adds a method to normalize the original type inheritance. This method is Object. create (), this method has two parameters. The first is the object as the new object prototype, like the person above, and the second is the object that defines additional attributes for the new object. The second parameter is optional.
For example:
var person_one = Object.create(person, { name: { value:"Jon" }});
If you only want to keep one object similar to another, you can use the original type inheritance.
Parasitic inheritance
Implement an original type inheritance in a function, and then add your own attributes and methods for the received object.
For example:
function createAnother(o){ var person_one = personObject(o); person_one.sayHi = function(){ alert(" hi "); } return person_one;}
Parasitic combined inheritance
Combination inheritance also has its disadvantages. It implements two property inheritance, and parasitic combination inheritance avoids this problem. The instance inherits attributes through constructor, while the prototype method inherits attributes through parasitic inheritance.
For example:
function inherit(subType, superType){ var prototype = Object(superType.prototype); prototype.constructor = subType; subType.prototype = prototype;}
By calling the above function, the prototype of subTye. prototype is superType. prototype, and the inheritance of the prototype method is completed.
The above javascript inheritance Parsing is a small part of the Content shared to you, I hope to give you a reference, but also hope you can support the house of helping customers.