ExtJs入门,类、语法、函数

来源:互联网 发布:java判断是否为素数 编辑:程序博客网 时间:2024/05/20 16:11

1.mixins的使用

Ext.define('Eat',{

    eat:function(value){return '吃:' + value;}

});

Ext.define('Drink',{

    drink:function(value){return '喝:' + value;}

});

Ext.define('Play',{

    play:function(value){return '玩:' + value;}

});

Ext.define('DoSomeThing',{

    mixins:{

        Eat:'Eat',

        Drink:'Drink',

        Play:'Play'

    },

    doSth:function(value,type){

         if(type == 'eat'){

              return this.eat(value);

        }else if(type == 'drink'){

             return this.drink(value);

        }else{

            return this.mixins.Play.play.call(this,value);

       }

    }

});

 

var doSomeThing = new DoSomeThing();

doSomeThing.doSth('苹果','eat');    // 吃:苹果

doSomeThing.doSth('牛奶','drink'); // 喝:牛奶

doSomeThing.doSth('篮球','play');  // 玩:篮球

 

mixins:是为类添加特殊的功能,将多个类混合使用,使用类里可以直接调用被引用类的方法

 

2.config的使用

Ext.define('DoSomeThing',{
 config:{
  type
 },
 typeValue: '吃:',
 constructor:function(config){
  this.initConfig(config);
  return this;
 },
 applyType: function(type){
  if(type == 'eat'){
   this.typeValue = '吃:';
  }else if(type == 'drink'){
   this.typeValue = '喝:';
  }else if(type == 'play'){
   this.typeValue = '玩:';
  }
 },
 doSth: function(value){
  return this.typeValue + value;
 }
});

var doSomeThing = new DoSomeThing();
doSomeThing.applyType('eat');
doSomeThing.doSth('苹果');    // 吃:苹果
doSomeThing.applyType('drink');
doSomeThing.doSth('牛奶'); // 喝:牛奶
doSomeThing.applyType('play');
doSomeThing.doSth('篮球');  // 玩:篮球

 

config:config里的属性在执行initConfig后都会生成applyXX、setXX、getXX、resetXX四个函数,XX变了后会触发applyXX方法

 

3.statics的使用

Ext.define('DoSomeThing',{
 statics:{
  eat:function(value){
   return '吃:'+value;
  },
  drink:function(value){
   return '喝:'+value;
  },
  play:function(value){
   return '玩:'+value;
  }
 },
 constructor:function(config){
  return this;
 }
});
var doSomeThing = new DoSomeThing();
doSomeThing.eat('苹果');    // 吃:苹果
doSomeThing.drink('牛奶'); // 喝:牛奶
doSomeThing.play('篮球');  // 玩:篮球

 

statics:statics的方法不会在累的原型里生成。

原创粉丝点击