JS类中event的简单实现

None.gif <!--  
None.gif    description:JS类中event的简单实现
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 Event  </ 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
function RaiseEvent(sender,event)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    event.replace(
/^undefined|null|\s+/,"");
InBlock.gif    eval(
"var f = function(sender){\n"+event+"\n}");
InBlock.gif    f(sender);
ExpandedSubBlockEnd.gif}

InBlock.gif
function EventHandler(o)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
this.delegate = "\n";
InBlock.gif
InBlock.gif    
//function
InBlock.gif
    if(typeof o=="function")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.delegate += o.toString().match(/function([^\0\(]*)\(/)[1+ "(sender);\n";
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//function name
InBlock.gif
    else if(typeof o=="string" && /^[a-z\$]*[0-9a-z]+$/i.test(o))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.delegate += o + "(sender);\n";
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//function code
InBlock.gif
    else if(typeof o=="string")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.delegate += o + ";\n";
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//else
InBlock.gif
    else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//do nothing
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
return this.delegate;
ExpandedSubBlockEnd.gif}

InBlock.gifEventHandler.prototype.toString 
= function()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
return this.delegate;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif
//示例
ExpandedSubBlockStart.gifContractedSubBlock.gif
function Persion()dot.gif{}
InBlock.gifPersion.prototype.onBeginSpeak;
InBlock.gif
//也可有默认事件,优先进行处理
InBlock.gif
Persion.prototype.onEndSpeak = new EventHandler(DefaultEvent);
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);
InBlock.gif
InBlock.gif    RaiseEvent(
thisthis.onEndSpeak);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
var p = new Persion()
InBlock.gif
InBlock.gif
//事件挂接,可以挂接多个
InBlock.gif
p.onBeginSpeak += new EventHandler(BeginSpeak1);
InBlock.gifp.onBeginSpeak 
+= new EventHandler(BeginSpeak2);
InBlock.gifp.onEndSpeak 
+= new EventHandler("EndSpeak");
InBlock.gifp.onEndSpeak 
+= new EventHandler("alert('讲话结束了,大家散场吧。');");
InBlock.gif
InBlock.gifp.Speak(
"大家好");
InBlock.gif
InBlock.gif
function BeginSpeak1(sender)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    alert(
"处理BeginSpeak事件!--1\n讲话者:"+sender.name);
ExpandedSubBlockEnd.gif}

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

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

InBlock.gif
function DefaultEvent(sender)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    alert(
"EndSpeak的默认事件处理。");
ExpandedSubBlockEnd.gif}

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

转载于:https://www.cnblogs.com/Lonsan/archive/2005/04/07/js.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值