Classes and Object in Extjs

Posted on Updated on

API of Ext JS has been designed specially for developers who are working with Object-oriented(OO) languages. Libraries are organized into packages and classes.

We will be discussing below listed topic.

  1. API Structure
  2. Classes and Objects
  3. Constructor
  4. Property
  5. Config
  6. Method

1) API Structure

API is grouped into packages similar to Java. Every package contains a collection of classes, whose names begin with the letters Ext. Examples are Ext.model.Model, Ext.store.Store etc. technically Ext is a JavaScript object that houses all other classes in Ext JS.

Ext is defined as
var Ext = {};

The general format of any class name is
Ext.packageName.optionalSubPackageName.ClassName

Package names begin with a lowercase character and Class names begin with an uppercase character. The names follow the “CamelCase” naming convention similar to Java. Ext.Base is the base class for all the classes in Ext JS.

2) Classes and Objects

We can create a new class in Ext JS using Ext.define method.

“School” is the name of class which is inside package “com.code2succeed”and we can pass object as argument while defining a class.

We can create an object of the School class using Ext.create method as shown here.

Also we can use new keyword to create an object of the School class.

3) Constructor

Similar to Java, In Ext JS constructor of the class get invoked whenever we are creating an object of the class.

On creating an object of class com.code2succeed.School the constructor gets invoked and School created printed on the console.

4) Property

Similar to instance variable in Java, we can define variables in Ext JS.

In above example we have created School class with 2 property name and noOfFaculties. Properties are initialized using this keyword, which make them visible to object of the class.

Now create object of School class using below code.

The above code snippet will print following on console.

5) Config

Ext JS  provides a config section for every class where we can list the attributes of the class with default values.

In the code snippet above, we’ve defined a School class with name and noOfFaculties listed in the config section. The config section is initialized in the constructor by calling the initConfig method. The initConfig method that is present in Ext.Base class initializes the configuration object.

The creation of object for class School will remain the same.

6) Method

We can define custom methods in a class.

We have defined a custom method display in the School class which can be accessed using the object reference.

The above code snippet will print following on console.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.