Javascript Object-oriented Inheritance

Source: Internet
Author: User

Class inheritance implementation mechanism in prototype framework CopyCode The Code is as follows: // Add a static method for the object class: Extend
Object. Extend = function (destination, source ){
For (property in source ){
Destination [property] = source [property];
}
Return destination;
}
// Use the object class to add the extend method for each object
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. It is used to assign all the properties of source in the parameter to the destination object and return the reference of destination. The following describes the implementation of object. Prototype. Extend. Because object is the base class of all objects, an extend method is added for all objects. The statement in the function body is as follows:
Object. Extend. Apply (this, [This, object]);
This statement runs the static method of the object class as the method of the object. The first parameter This is pointing to the object instance itself, and the second parameter is an array containing two elements: object itself and the passed object parameter object. The function assigns all attributes and methods of the parameter object to the object that calls the method and returns its reference. With this method, we can see the implementation of class inheritance below: Copy code The Code is as follows: <script language = "JavaScript" type = "text/JavaScript">
<! --
// Define the extend Method
Object. Extend = function (destination, source ){
For (property in source ){
Destination [property] = source [property];
}
Return destination;
}
Object. Prototype. Extend = function (object ){
Return object. Extend. Apply (this, [This, object]);
}
// Define class1
Function class1 (){
// Constructor
}
// Define the class class1 Member
Class1.prototype = {
Method: function (){
Alert ("class1 ");
},
Method2: function (){
Alert ("method2 ");
}

}
// Define class2
Function class2 (){
// Constructor
}
// Let class2 inherit from class1 and define new members
Class2.prototype = (New class1 (). Extend ({
Method: function (){
Alert ("class2 ");
}
});

// Create two instances
VaR obj1 = new class1 ();
VaR obj2 = new class2 ();
// Test the obj1 and obj2 Methods
Obj1.method ();
Obj2.method ();
Obj1.method2 ();
Obj2.method2 ();
// -->
</SCRIPT>

The running result shows that the inheritance is correctly implemented, and the additional members of the derived class can also be defined in the form of a list.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.