The implementation principle of JS Object-oriented Inheritance is as follows: 1. use the call () method (Object impersonating Mechanism) to inherit the attributes of the parent class from call ([thisObj [, arg1 [, arg2 [,[,. argN]) & amp; nbsp; call one of an object
The implementation principle of JS Object-oriented Inheritance is as follows:
1. Use the call () method (Object impersonating Mechanism) to inherit the attributes of the parent class
Call ([thisObj [, arg1 [, arg2 [, [,. argN]) call a method of an object to replace the current object with another object.
2. inherit the parent class method through the prototype attribute (prevent duplicate copies of the method from being created, resulting in a waste of resources)
Example:
/*** Js Object-oriented Inheritance ** @ link http://www.phpddt.com * // The parent class function Person (name, age) {this. name = name; this. age = age;} Person. prototype. showName = function () {alert (this. name);} Person. prototype. showAge = function () {alert (this. age);} // subclass function Student (name, age, grade) {Person. call (this, name, age); this. grade = grade;} for (var I in Person. prototype) {Student. prototype [I] = Person. prototype [I]; // inherits the members of the prototype chain} // The new member Method Student. prototype. showGrade = function () {alert (this. grade);} var p1 = new Person ("James", "20"); p1.showName (); var p2 = new Student ("Xiaohong", "21 ", "80 points"); p2.showName (); p2.showGrade ();