Home  >  Article  >  Web Front-end  >  javascript object-oriented inheritance_javascript skills

javascript object-oriented inheritance_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:41:00995browse

The class inheritance implementation mechanism in the prototype framework

Copy code The code is as follows:

// is Add static methods to the Object class: extend
Object.extend = function(destination, source) {
for(property in source) {
destination[property] = source[property];
}
return destination;
}
//Add method extend for each object through the Object class
Object.prototype.extend = function(object) {
return Object.extend.apply(this, [ this, object]);
}

The Object.extend method is easy to understand. It is a static method of the Object class and is used to assign all properties of the source in the parameter to the destination object. , and returns a reference to destination. Let’s explain the implementation of Object.prototype.extend. Because Object is the base class of all objects, here is an extend method added for all objects. The statements in the function body are as follows:
Object.extend.apply(this ,[this,object]);
This sentence is to run the static method of the Object class as a method of the object. The first parameter this points to the object instance itself; the second parameter is an array, including two elements: The object itself and the object parameter object passed in. The function is to assign all properties and methods of the parameter object object to the object itself that calls the method, and return a reference to itself. With this method, let’s look at the implementation of class inheritance:
Copy the code The code is as follows: