基础知识:javascript
javascript对象:java
var mayHash={ide
str_att:"a temp",this
int_att:7,spa
bool_att:false,code
undefined_att:null,对象
hash_att:{},blog
fun_att:function(){}继承
};ip
模拟类与继承》
javascript是一门基于对象的语言,对象能够继承自其余对象,可是javascript采用的是基于原型的继承机制:
1.dojo定义简单类:
1 dojo.declare( 2 "Shape",//类名 3 null,//无父类,为null 4 { 5 color: 0, 6 setColor: function (colors) { 7 this.color = colors; 8 } 9 } 10 );
2.单继承:
1 dojo.declare( 2 "Circle",//类名 3 Shape,//父类 4 { 5 radius: 0, 6 constructor: function (radius) { 7 this.radius = radius || this.radius; 8 }, 9 setRadius: function (radius) { 10 this.radius = radius; 11 }, 12 area: function () { 13 return Math.PI * this.radius * this.radius; 14 } 15 } 16 );
3.扩展父类方法
1 setColor: function (colors) { 2 var total = ((colors & 0xFF0000) >> 16) + ((colors & 0x00FF00) >> 8) + (colors & 0x0000FF); 3 if (colors > 350) { 4 //调用父类中的同名方法使用:this.inherited(argument) 5 this.inherited(arguments); 6 } 7 },
4.添加或者修改基类方法
1 //添加基类方法 2 dojo.extend(Shape, 3 { 4 setBorderStyle: function (style) { 5 this.BorderStyle = style; 6 } 7 });
5.多继承
1 //position类 2 dojo.declare( 3 "Position",//类名 4 null,//无父类 5 { 6 x: 0, 7 y: 0, 8 9 constructor: function (x, y) { 10 this.x = x || this.x; 11 this.y = y || this.y; 12 }, 13 14 setPosition: function (x, y) { 15 this.x = x; 16 this.y = y; 17 }, 18 19 movePosition: function (x, y) { 20 this.x = x; 21 this.y = y; 22 } 23 } 24 ); 25 26 //2.positionCircle类 27 dojo.declare( 28 "PositionCircle",//类名 29 [Circle,Position],//Circle父类,Position为Mixin类 30 { 31 constructor: function (radius, x, y) { 32 this.setPosition(x, y); 33 } 34 } 35 )