1 // 定义一个猫的类
// 定义一个猫的类
2 function Cat(description) {
function Cat(description) {
3 // 定义一个事件
    // 定义一个事件
4 this.description = description;
    this.description = description;
5 this.cry = null;
    this.cry = null;
6 }
}
7 // 添加事件
// 添加事件
8 Cat.prototype.AddHandler = function(handler) {
Cat.prototype.AddHandler = function(handler) {
9 if (this.cry == null) {
    if (this.cry == null) {
10 this.cry = new Array();
        this.cry = new Array();
11 this.cry[this.cry.length] = handler;
        this.cry[this.cry.length] = handler;
12 } else {
    } else {
13 this.cry[this.cry.length] = handler;
        this.cry[this.cry.length] = handler;
14 }
    }
15 }
}
16 // 定义猫叫的方法
// 定义猫叫的方法
17 Cat.prototype.OnCry = function() {
Cat.prototype.OnCry = function() {
18 alert("Cat:miao miao
    alert("Cat:miao miao .");
.");
19 for (var i = 0;i < this.cry.length; i++) {
    for (var i = 0;i < this.cry.length; i++) {
20 this.cry[i](this);
        this.cry[i](this);
21 }
    }
22 }
}
23 // 定义一个老鼠类,老鼠对猫的叫感兴趣,所以就注册了cry事件
// 定义一个老鼠类,老鼠对猫的叫感兴趣,所以就注册了cry事件
24 function Mouse() {
function Mouse() {
25 }
}
26 Mouse.prototype.Run = function(des) {
Mouse.prototype.Run = function(des) {
27 switch (des.description) {
    switch (des.description) {
28 case "laze" :
        case "laze" :
29 alert("操,你吓我是吧,你能追的上我吗,我就不跑!");
            alert("操,你吓我是吧,你能追的上我吗,我就不跑!");
30 break;
            break;
31 case "smark" :
        case "smark" :
32 alert("我日,你来了呀,你NB,我走!");
            alert("我日,你来了呀,你NB,我走!");
33 break;
            break;
34 }
    }
35 }
}
36 var c = new Cat('laze');
var c = new Cat('laze');
37 var c2 = new Cat('smark');
var c2 = new Cat('smark');
38 var m = new Mouse();
var m = new Mouse();
39 c.AddHandler(m.Run);
c.AddHandler(m.Run);
40 c2.AddHandler(m.Run);
c2.AddHandler(m.Run);
41 // 先让懒猫先叫
// 先让懒猫先叫
42 c.OnCry();
c.OnCry();
43 // 再让NB的猫再叫
// 再让NB的猫再叫
44 c2.OnCry();
c2.OnCry();
 
 // 定义一个猫的类
// 定义一个猫的类2
 function Cat(description) {
function Cat(description) {3
 // 定义一个事件
    // 定义一个事件4
 this.description = description;
    this.description = description;5
 this.cry = null;
    this.cry = null;6
 }
}7
 // 添加事件
// 添加事件8
 Cat.prototype.AddHandler = function(handler) {
Cat.prototype.AddHandler = function(handler) {9
 if (this.cry == null) {
    if (this.cry == null) {10
 this.cry = new Array();
        this.cry = new Array();11
 this.cry[this.cry.length] = handler;
        this.cry[this.cry.length] = handler;12
 } else {
    } else {13
 this.cry[this.cry.length] = handler;
        this.cry[this.cry.length] = handler;14
 }
    }15
 }
}16
 // 定义猫叫的方法
// 定义猫叫的方法17
 Cat.prototype.OnCry = function() {
Cat.prototype.OnCry = function() {18
 alert("Cat:miao miao
    alert("Cat:miao miao .");
.");19
 for (var i = 0;i < this.cry.length; i++) {
    for (var i = 0;i < this.cry.length; i++) {20
 this.cry[i](this);
        this.cry[i](this);21
 }
    }22
 }
}23
 // 定义一个老鼠类,老鼠对猫的叫感兴趣,所以就注册了cry事件
// 定义一个老鼠类,老鼠对猫的叫感兴趣,所以就注册了cry事件24
 function Mouse() {
function Mouse() {25
 }
}26
 Mouse.prototype.Run = function(des) {
Mouse.prototype.Run = function(des) {27
 switch (des.description) {
    switch (des.description) {28
 case "laze" :
        case "laze" :29
 alert("操,你吓我是吧,你能追的上我吗,我就不跑!");
            alert("操,你吓我是吧,你能追的上我吗,我就不跑!");30
 break;
            break;31
 case "smark" :
        case "smark" :32
 alert("我日,你来了呀,你NB,我走!");
            alert("我日,你来了呀,你NB,我走!");33
 break;
            break;34
 }
    }35
 }
}36
 var c = new Cat('laze');
var c = new Cat('laze');37
 var c2 = new Cat('smark');
var c2 = new Cat('smark');38
 var m = new Mouse();
var m = new Mouse();39
 c.AddHandler(m.Run);
c.AddHandler(m.Run);40
 c2.AddHandler(m.Run);
c2.AddHandler(m.Run);41
 // 先让懒猫先叫
// 先让懒猫先叫42
 c.OnCry();
c.OnCry();43
 // 再让NB的猫再叫
// 再让NB的猫再叫44
 c2.OnCry();
c2.OnCry(); 
                    
                     
                    
                 
                    
                 


 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号