Inheritance of JavaScript and extjs Ext. Extend Ext. applyif

Source: Internet
Author: User

First, let's take a look at the typical JavaScript Inheritance Mechanism.

Define a class named baseclass, and then define two functions for baseclass: somemethod () and overridenmethod ().

VaR Baseclass =   Function (){
// Do something
};
Baseclass. Prototype. somemethod =   Function (){
// Do something
};
Baseclass. Prototype. overridenmethod =   Function (){
// Do something
};

Using traditional methods to implement class inheritance in Javascript

VaR Subclass =   Function (){
Baseclass. Call ( This );
};
Subclass. Prototype =   New Baseclass ();
Subclass. Prototype. newmethod =   Function (){
// Do something
};
Subclass. Prototype. overridenmethod =   Function (){
// Do something
};

In the subclass constructor, you first call the baseclass constructor to initialize data, and then use

Subclass. Prototype = new baseclass ();

This statement allows the subclass class to obtain all attributes and functions in baseclass, so as to implement the inheritance operation in Javascript. It adds a new function to the subclass and overwrites the Same Name function in the parent class.

Use the Ext. Extend () function in extjs to implement the inheritance Function

VaR Subclass =   Function (){
Subclass. superclass. constructor. Call ( This );
};
Ext. Extend (subclass, baseclass ,{
Newmethod: Function (){};
Overridenmethod: Function (){};
};

Ext. the extend () function provides a way to directly access the parent class constructor through subclass. superclass. constructor. call (this); you can directly call the constructor of the parent class. The first parameter of this function is always this to ensure that the constructor of the parent class works in the scope of the subclass.
If the constructor of the parent class needs to input parameters, you can directly pass the required parameters to it:
Subclass. superclass. constructor. Call (this, config); in this way, a subclass that inherits all attributes and functions of the parent class is obtained.

The Ext. Apply function is used to copy all attributes of an object to another object.
Ext. applyif is similar to Ext. apply. The difference is that if an attribute already exists in the target object, ext. applyif does not overwrite it.

 

Related Article

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.