dojo学习(1)

基础知识:

javascript对象:

var mayHash={

  str_att:"a temp",

  int_att:7,

  bool_att:false,

  undefined_att:null,

  hash_att:{},

  fun_att:function(){}

};

模拟类与继承》

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     );
View Code

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     );
View Code

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         },
View Code

4.添加或者修改基类方法

1 //添加基类方法
2 dojo.extend(Shape,
3     {
4         setBorderStyle: function (style) {
5             this.BorderStyle = style;
6         }
7     });
View Code

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     )
View Code

 

posted @ 2014-03-24 17:29  H·T·K  阅读(219)  评论(0编辑  收藏  举报