JS类中event的简单实现(续)

None.gif <!--  
None.gif    description:JS类中event的简单实现2
None.gif    code by Lonsan on 2005
None.gif    email:Lonsan21@163.com
None.gif
None.gif    请各位多批评指导,谢谢
None.gif
-->
None.gif
< html >
None.gif
< head >
None.gif
< title >  JS Event2  </ title >
None.gif
< style >
None.gif
</ style >
None.gif
</ head >
None.gif
None.gif
< body >
None.gif
</ body >
None.gif
</ html >
None.gif
ExpandedBlockStart.gifContractedBlock.gif
< script  language ="javascript" > dot.gif
InBlock.gif
<!--
InBlock.gif
//实现,实现类似C#风格调用
InBlock.gif//
已经支持+=和-=操作符,但是还存在很多问题,正寻求解决方法 
InBlock.gif//
注:参考了birdshome提示,在此表示感谢。
InBlock.gif

InBlock.gif
function RaiseEvent(sender,event)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
var funs="";
InBlock.gif    
for(var k in EventHandler.Events)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(k=="length")
InBlock.gif            
continue;
InBlock.gif        
InBlock.gif        
if((event&EventHandler.Events[k].ID)>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            funs 
+= k+"(sender);\n";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    eval(
"var f = function(sender){\n"+funs+"\n}");
InBlock.gif    f(sender);
ExpandedSubBlockEnd.gif}

InBlock.gif
function EventHandler(o)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
this.EventID;
InBlock.gif
InBlock.gif    
var fname;
InBlock.gif    
if(typeof o=="function")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        fname 
= o.toString().match(/function([^\0\(]*)\(/)[1];
InBlock.gif        
if(EventHandler.Events[fname]==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.EventID = (1<<EventHandler.Events.length);
ExpandedSubBlockStart.gifContractedSubBlock.gif            EventHandler.Events[fname] 
= dot.gif{ID:this.EventID};
InBlock.gif            EventHandler.Events.length
++;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.EventID = EventHandler.Events[fname].ID;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gifEventHandler.Events
=dot.gif{length:0};
InBlock.gifEventHandler.Null
=0;
InBlock.gifEventHandler.prototype.toString 
= function()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
return this.EventID;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//
InBlock.gif

InBlock.gif
//示例
ExpandedSubBlockStart.gifContractedSubBlock.gif
function Persion()dot.gif{}
InBlock.gifPersion.prototype.onBeginSpeak
=EventHandler.Null;
InBlock.gifPersion.prototype.name
="Lonsan";
InBlock.gifPersion.prototype.Speak 
= function(sWords)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    RaiseEvent(
thisthis.onBeginSpeak);
InBlock.gif
InBlock.gif    alert(
"正在讲话。。。\n以下是讲话内容:\n"+sWords);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
var p = new Persion()
InBlock.gif
InBlock.gif
//test1
InBlock.gif
p.onBeginSpeak += new EventHandler(BeginSpeak1);
InBlock.gifp.onBeginSpeak 
+= new EventHandler(BeginSpeak2);
InBlock.gif
InBlock.gifalert(
"测试1开始!!");
InBlock.gifp.Speak(
"大家好,我是"+p.name+"");
InBlock.gif
InBlock.gif
InBlock.gif
//test2
InBlock.gif
p.onBeginSpeak -= new EventHandler(BeginSpeak2);
InBlock.gifp.onBeginSpeak 
+= new EventHandler(BeginSpeak3);
InBlock.gif
InBlock.gifalert(
"测试2开始!!");
InBlock.gifp.Speak(
"第二次\n已经取消BeginSpeak2事件。\n我是"+p.name+"");
InBlock.gif
InBlock.gif
function BeginSpeak1(sender)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    alert(
"处理BeginSpeak事件!--1\n");
ExpandedSubBlockEnd.gif}

InBlock.gif
function BeginSpeak2(sender)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    alert(
"处理BeginSpeak事件!--2");
ExpandedSubBlockEnd.gif}

InBlock.gif
function BeginSpeak3(sender)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    sender.name
="(Lonsan)蓝山";
InBlock.gif    alert(
"处理BeginSpeak事件!--3");
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif
//-->
None.gif
</ script >

转载于:https://www.cnblogs.com/Lonsan/archive/2005/04/08/js2.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值