ExtJsなどの定義

1908 ワード

クラスの定義:

Ext.define("Calulator",{
  constructor:function(){
    return this;
  },
  plus:function(v1,v2){
    return v1+v2;
  },
  minus:function(v1, v2){
    return v1-v2;
  },
  
  multiply:function(v1, v2){
  return v1*v2;
}

});

var cal = new Calulator();
console.log(cal.plus(12,10));
console.log(cal.minus(12,10));
console.log(cal.multiply(12,10));
---------------------------------
定義クラスの継承:

Ext.define("NewCal",{
  extend:"Calulator",
  hex:function(v1){
    return v1.toString(16);
  }
});

var newCal = new NewCal();
console.log(newCal.hex(10));
console.log(newCal.plus(10,10));
--------------------------
mixinsを利用してクラスを混合します.

Ext.define("HEX",{
  hex:function(v3){
    return v3.toString(16);
  }
});

Ext.define("BIN",{
  bin:function(v1){
    return v1.toString(2);
  }
});

Ext.define("OCT",{
  oct:function(v1){
    return v1.toString(8);
  }
});

Ext.define("NewCal2",{
  extend:"Calulator",
  mixins:{
    Hex:"HEX",
    Bin:"BIN",
    Oct:"OCT"
  },
  convert:function(vaule,type){
    switch(type){
      case 2:
        return this.bin(vaule);
        break;
      case 16:
        return this.hex(vaule);
        break;
      case 8:
        return this.oct(vaule);
        break;
    }
  }
});
var newCal2 = new NewCal2();
console.log(newCal2.convert(10,2));
console.log(newCal2.convert(10,8));
console.log(newCal2.convert(10,16));