Original Type inheritance
The original type inheritance is similar to a single inheritance. It uses the prototype attribute. prototype can replicate an object, but it does not inherit attributes from other prototypes or constructors, attributes are inherited from the actual object.
// Create the person constructor function person (name) {This. name = Name;} // person to add a new method person. prototype. getname = function () {return this. name ;}; // create the user constructor function user (name, password) {// Note: convenience overload and inheritance are not supported here, that is to say, you cannot call the constructor of the parent class this. name = Name; this. password = PASSWORD ;}; // user inherits all the methods of the person Object User. prototype = new person (); // Add a new method to the user object. prototype. getPassword = function () {return this. password ;};
The most important line in the preceding example is user. Prototype = new person ();. user is a reference to the user object constructor. New person () creates a new person object using the person constructor, and sets the user constructor prototype as the result of this operation. That is to say, every time a new user () occurs, the new user object will carry all the methods of the person object, just as it is obtained by operating new person.
Class inheritance
JS does not support class inheritance like Java and C ++, but this inheritance method is very convenient to use. Currently, many JS class libraries provide similar solutions. Let's take a look at how the prototype library is implemented.
// Create a global object named 'class' var class ={// it only has one function and its role is to create a new object constructor create: function () {// create an anonymous object constructor return function () {// call its own initialization method this. initialize. apply (this, arguments) ;}}// Add a new static method to the object. Its function is to copy attributes from one object to another. extend = function (destination, source) {// traverses all attributes to be extended for (property in source) {// then add them to the target object destination [property] = source [property];} // return the modified object return destination ;}
Use it for a moment:
// Create a new empty person object var person = Class. create (); // copy the following function to object in prototype of person. extend (person. prototype, {// The person constructor calls initialize: function (name) {This. name = Name ;}, // simple function of the person object getname: function () {returnthis. name ;}}); // create a user object var user = class with an empty constructor. create (); // The user object inherits all the function users of the parent class. prototype = object. extend (new person (), {// reload the initialization function of the parent class to the new initialize: function (name, password) {This. name = Name; this. password = password;}, // Add a new function GetPassword: function () {returnthis to this object. password ;}}); // check the two classes created. var person = new person ("Lisa"); var user = new user ("Lisa ", "123 ");
Prototype does use only two simple functions to create and manage the entire object-oriented structure. The starting point of these two functions is very simple:
Class. Create (): The entire function uses only one anonymous function as the constructor. This simple constructor only does one thing-calls and executes the initialize attribute of the object. If the object does not contain the initialize attribute, an exception is thrown.
Object. Extend (): This only copies all attributes from one object to another.
Although this technology is complex, it provides basic functions required by object-oriented programming.