基礎知識: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 )