The class inheritance implementation mechanism in the prototype framework
// 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: