In JavaScript, there are no exends keywords in Java, and inheritance relationships can only be implemented in other ways.
1) Object Impersonation
1 functionParent (username)2 {3 This. Username =username;4 5 This. SayHello =function()6 {7Alert This. username);8 }9 }Ten One functionChild (username, password) A { - //The following three lines of code are the key code that implements the child's inherited parent - This. method =Parent; the This. Method (username); - Delete This. method; - - This. Password =password; + - This. Sayworld =function() + { AAlert This. password); at } - } - - varParent =NewParent ("Zhangsan"); - varChild =NewChild ("Lisi", "1234"); - Parent.sayhello (); in Child.sayhello (); -Child.sayworld ();
2) Call method mode.
The call method is a method in a function object, so each function that we define has that method. The call method can be called by a function name, and the first parameter of the calling method is passed to this in the function, starting with the 2nd argument and assigning it to the arguments in the function one by two.
1 //implementing object inheritance by using the call method2 3 functionParent (username)4 {5 This. Username =username;6 7 This. SayHello =function()8 {9Alert This. username);Ten } One } A - functionChild (username, password) - { the //The following line of code is the first way to replace the three-line key code -Parent.call ( This, username); - - This. Password =password; + - This. Sayworld =function() + { AAlert This. password); at } - } - - varParent =NewParent ("Zhangsan"); - - varChild =NewChild ("Lisi", "123"); in - Parent.sayhello (); to + Child.sayhello (); -Child.sayworld ();
3) How to apply method
1 //implementing object inheritance using the Apply method2 functionParent (username)3 {4 This. Username =username;5 6 This. SayHello =function()7 {8Alert This. username);9 }Ten } One A functionChild (username, password) - { -Parent.apply ( This,NewArray (username)); the - This. Password =password; - - This. Sayworld =function() + { -Alert This. password); + } A } at - varParent =NewParent ("Zhangsan"); - varChild =NewChild ("Lisi", "123"); - - Parent.sayhello (); - in Child.sayhello (); -Child.sayworld ();
4) prototype chain mode (cannot pass parameters to the constructor function)
1 functionParent ()2 {3 4 }5 6Parent.prototype.hello = "Hello";7Parent.prototype.sayHello =function()8 {9Alert This. Hello);Ten } One A functionChild () - { - the } - -Child.prototype =NewParent (); - +Child.prototype.world = "World"; -Child.prototype.sayWorld =function() + { AAlert This. World); at } - - varChild =NewChild (); - - Child.sayhello (); -Child.sayworld ();
5) mixing mode (recommended)
1 functionParent (Hello)2 {3 This. Hello =Hello;4 }5 6Parent.prototype.sayHello =function()7 {8Alert This. Hello);9 }Ten One functionChild (Hello, world) A { -Parent.call ( This, hello); - the This. World =World ; - } - -Child.prototype =NewParent (); + -Child.prototype.sayWorld =function() + { AAlert This. World); at } - - varChild =NewChild ("Hello", "World"); - - Child.sayhello (); -Child.sayworld ();
Inheritance in JavaScript