首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用extjs4.2语法定义类

用extjs4.2语法定义类
EN

Stack Overflow用户
提问于 2015-11-04 09:02:40
回答 1查看 47关注 0票数 0

我正在研究extjs4.2,在语法和跟踪方面,在定义类和创建对象方面,我确实需要一些帮助。有什么帮助吗?

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-04 12:58:24

关于代码的一些注释,以便您能够更好地理解它:

代码语言:javascript
复制
// 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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33517604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档