mikeb
mikeb

Reputation: 727

Define a class in extjs4.2 syntax

I'm trying to study extjs 4.2, and i could really use some help when it comes to syntax and tracing, in defining a class and creating objects. Any help please?

Ext.define('Student', {
    name: 'unnamed',
    getName: function() {
        alert('Student name is ' + this.name);
    },
    constructor: function(studentName) {
        if (studentName) this.name = studentName;
    },
    statics: {
        staticMethod: function() {
            alert("This is static method of student class");
        }
    }
}

);

//create an object using 'new'
var student1 = new Student('ABC');
student1.getName();

//create an object using Ext.create
var student2 = Ext.create('Student', 'XYZ');
student2.getName();

//create an object using className.create()
var student3 = Student.create('123');
student3.getName();

//call static method by className.staticMethodName()
Student.staticMethod();

Upvotes: 0

Views: 53

Answers (1)

Michel
Michel

Reputation: 571

Some comment on the code, so that you could better understand it:

// Class Student definition
Ext.define('Student', {
    name: 'unnamed',        // a basic string property
    getName: function() {   // a basic function to display a message with the name
        alert('Student name is ' + this.name);
    },

    constructor: function(studentName) {    // The constructor function with the name of the student as parameter
        if (studentName) this.name = studentName;   // If the name is given then assign it to the instance
    },

    statics: {      // Begin of static functions declaration

        staticMethod: function() {  // a static function called "staticMethod"
            alert("This is static method of student class");
        }
    }               // End of static functions declaration
});
// End of class definition

//create an object using 'new' with the name "ABC"
var student1 = new Student('ABC');
student1.getName(); // Should display a message alert with text: "Student name is ABC"

//create an object using Ext.create
var student2 = Ext.create('Student', 'XYZ');    // The second parameter should be ignored
student2.getName(); // Should display a message alert with text: "Student name is Student"

//create an object using className.create()
var student3 = Student.create('123');
student3.getName(); // Should display a message alert with text: "Student name is 123"

//call static method by className.staticMethodName()
Student.staticMethod(); // call the static function declared in Student.
                        // This does not need any class instance

Upvotes: 2

Related Questions