Reference Link: http://yahaitt.iteye.com/blog/250338
Although the book has been on the way of inheritance, but these things every look at a little bit new harvest, so separate out to put.
1. Object Impersonation
functionParent (username) { This. Username =username; This. Hello =function() {alert ( This. username); }}functionChild (Username,password) {
//inheritance is implemented by appending the properties and methods of the parent to child in the following 3 rows //The first step: This.method is as a temporary property and points to the object that the parent points to, //Step Two: Execute the This.method method, which is the object function that the parent points to //Step three: Destroy the This.method attribute, which means that now child has all the properties and methods of the parent
This. method =Parent; This. method (username);//The most critical line Delete This. method; This. Password =password; This. World =function() {alert ( This. Password); } } varParent =NewParent ("Zhangsan"); varChild =NewChild ("Lisi", "123456"); Parent.hello (); Child.hello (); Child.world ();
2. Call
The call method is a method in a function class
The value of the first parameter of the call method is assigned to the this that appears in the class (that is, a method)
The second argument of the call method begins by assigning the parameter to the class (that is, the method) that is accepted
function Test (str) { alert (this. Name + ""+varnew= "Zhangsan"; Test.call (object,//
functionParent (username) { This. Username =username; This. Hello =function() {alert ( This. username); } } functionChild (Username,password) {Parent.call ( This, username); This. Password =password; This. World =function() {alert ( This. Password); } } varParent =NewParent ("Zhangsan"); varChild =NewChild ("Lisi", "123456"); Parent.hello (); Child.hello (); Child.world ();
3. Apply
The Apply method accepts 2 parameters,
A, the first parameter is the same as the first parameter of the call method, which is the value that appears in the class (that is, the method).
B, the second parameter is an array type, and each element in the arrays is assigned to a parameter that is accepted by the class (that is, the method)
functionParent (username) { This. Username =username; This. Hello =function() {alert ( This. username); } } functionChild (Username,password) {parent.apply ( This,NewArray (username)); This. Password =password; This. World =function() {alert ( This. Password); } } varParent =NewParent ("Zhangsan"); varChild =NewChild ("Lisi", "123456"); Parent.hello (); Child.hello (); Child.world ();
4. Prototype chain
That is, subclasses append all properties and methods appended by prototype in the parent class to child through prototype, thus implementing the inheritance
functionPerson () {} Person.prototype.hello= "Hello"; Person.prototype.sayHello=function() {alert ( This. Hello); } functionChild () {} Child.prototype=NewPerson ();//The function of this line is to append all attributes and methods appended by prototype to child in the parent, thus implementing the inheritanceChild.prototype.world = "World"; Child.prototype.sayWorld=function() {alert ( This. World); } varc =NewChild (); C.sayhello (); C.sayworld ();
5. Mixing mode
Hybrid call mode, prototype chain mode
functionParent (hello) { This. Hello =Hello;} Parent.prototype.sayHello=function() {alert ( This. Hello); } functionChild (Hello,world) {Parent.call ( This, hello);//inherit the properties of the parent class . This. World = World;//Add some properties} child.prototype=NewParent ();//inherit the methods of the parent class .Child.prototype.sayWorld=function(){//add some new methodsAlert This. World); } varc =NewChild ("Zhangsan", "Lisi"); C.sayhello (); C.sayworld ();
Go 5 ways to implement JS inheritance