Tuesday, September 11, 2012

Creating classes and objects with EXT JS 4.0


By nature JavaScript is a prototypal language. You can’t create classes like what you did with java,C# or something else. But you can simulate the class structure.

EXT JS is a JavaScript frame work. This will support OOP features and MVC in the JavaScript. Creating a class is so simple in EXT JS 4.

Ext.define is the method (EXT is base for all the classes in EXT JS for a while imagine Ext like $ in JQuery).

It takes 3 parameters
  1. String
  2. Object
  3. Function
Ext.define( String className, Object data, Function createdFn).

Ext.define(“Products”,{
name : “Apple”,
category : “Mobile”,
model : “iPhone4”,
showProductDetails : function(){
alert(this.name + this. Category + this.model);
}
});

you can create anonymous classes also. just pass a null instead of the class name.
 Ext.define(null, {
name : “Apple”,
category : “Mobile”,
model : “iPhone4”,
showProductDetails : function(){
alert(this.name + this. Category + this.model);
}     

});

to create the instance use Ext.create method.

var myProduct = Ext.create('Products');
myProduct.showProductDetails();//shows product details

No comments:

Post a Comment