extjs4: class system

来源:互联网 发布:js函数传对象参数 编辑:程序博客网 时间:2024/05/01 22:09
一:Configuration的用法 
Ext.define('My.first.Person',{  name : 'zhangsan',  age : 20,    // 1. 封装成员   // 2. 提供 'getter' ,'setter'方法;   // 3. 为每个config内的成员生成apply方法,这个方法在setter赋值前可以进行处理。  config : {           email : 'zhangsan@163.com',   others : {    height : 180,    belongsTo : 'china',   }  },    /*constructor : function(name, age){   if(name){ // 在创建实例时如果不传参数,会有默认的null参数传进来,所以要判断    this.name = name;    }   if(age){    this.age = age;   }  },*/    constructor : function(config){    this.initConfig(config);    },    applyEmail : function( aEmail ){     if(!aEmail){    alert('invalid email');   }else{    return aEmail; // 如果apply不返回参数,则setter不进行设置。   }  },  say : function(something){   alert( this.name + ' say ' + something + ' when he ' + this.age + ' years old!');  } });  var a = Ext.create('My.first.Person', 'lilei', '20');  //a.say('what'); a.setEmail('tupiku@yahoo.com'); //alert(a.getEmail()); a.setOthers({height:200, belongsTo:'Beijing'}); //alert(a.getOthers().height);  var b = Ext.create('My.first.Person',{  name : 'jack',  email : 'jack@soho.com', }) ///类中没有被config包裹的成员,不能被赋值 alert(b.name); // 'zhangsan' alert(b.email); // 'jack@soho.com'
二:statices的用法 
Ext.define('Computer',{  config : {   brand : 'ASUS',  },    statics : {   instanceCount : 0,   factory : function(computerBrand){    //静态方法中的this代表类本身    return new this({brand : computerBrand});   }  },    constructor : function(config){   this.initConfig(config);   this.self.instanceCount++; // self表示this的类  } });  dellComputer = Computer.factory('dell'); macComputer = Computer.factory('mac');  alert(macComputer.getBrand()); alert(Computer.instanceCount);