All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
动态网页变静态:ASP.NET生成静态HTML页_[Asp.Net教程]
sql_设置权限_数据库基础_mssql学习_编程技术
配置整合Win+Apache+PHP+MySQL+Tcomcat(或Resin)完全手册_[PHP教程]
SQL游标原理和使用方法_mssql学习_编程技术
asp.net得到全部已选中的RedioButton的值_[Asp.Net教程]
精通数据库系列之入门-基础篇2_mssql学习_编程技术
PHP实例:用PHP实现XML备份Mysql数据库_[PHP教程]
Asp.net日期字符串格式化显示方法_.net资料_编程技术
DOTNET:LINQ与ADO.NET,3.0的创新_.net资料_编程技术
ASP.NET程序中常用的三十三种代码2_[Asp.Net教程]
Reading number is top 10 pictures
A beautiful girl to bud2
刘亦菲写真集2
A beautiful girl to bud3
2012 national geographic daily picture2
The money of more than 100 countries and regions20
Absolutely shocked. National geographic 50 animal photographys9
Chinese paper-cut grilles art appreciation6
抗日又出雷剧情
王艳写真温柔如水1
Female star bikini
Download software ranking
Jinling thirteen stock
Boxer Classic video2
Ashlynn Video2
Unix video tutorial12
dreamweaver8中文版
功夫熊猫2(上集)
传奇私服架设教程-chm
Unix video tutorial10
Love the forty days
c#程序设计案例教程
aaa published in(发表于) 2013/12/13 9:41:07 Edit(编辑)
js也可以有自定义事件,注入就是这么爽_.net资料_编程技术

js也可以有自定义事件,注入就是这么爽_.net资料_编程技术

js也可以有自定义事件 注入就是这么爽_.net资料_编程技术-你的首页-uuhomepage.com

  在c#中有delegate,还有特殊的可以直接应用于事件编程的delegate,那就是event。而在js中没有c#的event,更没有delegate,有的只是dom元素内置的的native的不可扩展的event,比如无法为input元素添加事件,只能在其拥有的事件(如onclick=handler)上扩展应用。那么能不能做到自定义的事件模拟效果呢?答案是肯定的,也就是本文的主题。
  首先弄明白一下事件的意图——可以在发生一件事的时候执行额外的代码,如document.attachEvent('onclick', function(){alert('u click document')}),当点击页面时(事件发生了),就会执行我们为其挂接的其它代码(js中以function为语句集合,以下称为function),当然我们可以在一个事件上挂接任意多的function,这样就实现了一种灵活的可扩展编程接口。试想如果可以像在元素事件扩展应用一样可以在任意对象的任意方法上扩展,那对于js编程来讲就更加灵活了。先看一个例子,平时我们把相对对立的一个功能命名为一个function,并在需要的地方(通常是另一个function)调用以实现代码复用:
function F(){
this.method = function(){
alert('f.method is called')
g();
}
}
function g(){
alert(123)
}
var f = new F();
f.method()



我们把f.method中直接调用g改写一下,封装到一个Event对象中达到一样的效果,代码如下:
var Event = {
__list:[],
observe:function(obj, ev, fun){
this.__list.push({o:obj, e:ev, f:fun})
},
occor:function(obj, method){
var arr = []
for(var i=0; i if(this.__list[i].o==obj && this.__list[i].e==method) arr.push(this.__list[i]);
}
for(var i=0; i arr[i].f();
}
}
}


function F(){
this.method = function(){
alert('f.method is called')
Event.occor(this, 'method');
}
}


var f = new F();
Event.observe(f, 'method', function(){alert(123)})
f.method()这样乍看上去好像费了“太多”功夫,但却把“在f中调用g的写法”更通用化了,如果要在f中调用h则只需要多些一行Event.occor(this, 'methodName'),写到这里你肯定也注意到methodName的写法和最开始的写法是一样的,都是硬编的不具灵活性,如果在每个类的方法中都写入Event.occor(this, 'method')就太不雅观了,也背离了我们的初衷,动态修改一下method把它加到最后一行就ok了,下一步就是解决它,改进代码如下:


var Event = {
__list:[],
observe:function(obj, ev, fun){
this.__list.push({o:obj, e:ev, f:fun})
},
occor:function(obj, method){
var arr = []
for(var i=0; i if(this.__list[i].o==obj && this.__list[i].e==method) arr.push(this.__list[i]);
}
for(var i=0; i arr[i].f();
}
},
inject:function(obj){
for(var p in obj){
obj[p] = new Function(obj[p].toString().replace('function(){', '').replace('}', 'Event.occor(this,p)'))
}
}
}


function F(){
this.method = function(){
alert('f.method is called')
}
}


var f = new F();
Event.inject(f);
Event.observe(f, 'method', function(){alert(123)})
f.method()我们把显示的在被调用方法体内调用Event.occor改写到Event.inject中。到此我们就简单(还有一些安全代码没有处理,如没有判断obj[p]是否需要被改写、没有测试效率问题,没有处理更多添加Event.occor时的逻辑判断,下一步准备把它实现为一个Observeable对象,就更加灵活了)的完成了自定义事件。






添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.