2015-11-04 41 views
0

我正在學習extjs 4.2,在定義一個類和創建對象時,我真的可以在語法和跟蹤方面使用一些幫助。請幫忙嗎?在extjs4.2語法中定義一個類

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(); 
+1

你的問題,因爲它代表是不是真的清楚。你在問什麼代碼? –

+0

@EvanTrimboli這不是我的代碼,這是一個示例代碼,我正在尋找一個幫助,如果有可能任何人都可以跟蹤我這個代碼示例。我非常感謝。因爲我對這個概念有一個總體概念,但是我想深入瞭解代碼的細節,所以我會確保我完全理解它。 – mikeb

回答

2

在代碼中的一些評論,這樣你就可以更好地理解它:

// 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