*******************js闭包问题求解决******************

huangwenquan123 2011-07-09 02:42:40

addEvent:function(oTarget,name,funName){
var args = Array.prototype.slice.call(arguments).slice(3);
var regHandler=funName
if(args.length>0){
regHandler=function(e){
return funName.apply(this,[e||window.event].concat(args));
}//这里的regHandler产生了个闭包,一直在堆栈中存在,该怎么解决呢?
}
if(oTarget.addEventListener){
oTarget.addEventListener(name,regHandler,false);
}
else{
oTarget.attachEvent("on"+name,regHandler);
}
},
...全文
130 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangwenquan123 2011-07-10
  • 打赏
  • 举报
回复
大神都去玩回来了没!
豆虫 2011-07-09
  • 打赏
  • 举报
回复
闭包的东西 却是让很多人深受其害 我也是受害者之一 学习~~~~
sirzxj 2011-07-09
  • 打赏
  • 举报
回复
今天,去参加D2前端技术论坛了吧,见到几位大神
huangwenquan123 2011-07-09
  • 打赏
  • 举报
回复
//知道哪里有问题了,如果把
//this.addEvent(document,"mousemove",this.bindEvent(this,this.RightDownMouseMove));
//this.addEvent(document,"mouseup",this.bindEvent(this,this.RightDownMouseUp));
//这两个换成下面注释中的就可以正常运行,如果不换成注释的mouseup总是释放不了。是什么原因呢?
var Resize=Class.create();
Resize.prototype={
init:function(content,cut,cutImg){
this.content=content;
this.cut=cut;
this.cutImg=$(cutImg);
},
Set:function(funCase){
var fun;
switch(funCase){
case "RightDown":
fun=this.RightDownMouseDown;
this._obj=$(funCase);
break;
default:break;
}
this.addEvent(this._obj,"mousedown",this.bindEvent(this,fun))
},
RightDownMouseDown:function(){
var e=arguments[0]
this.para={};
this.para.mouseX=e.clientX;
this.para.mouseY=e.clientY;
this.para.cutWidth=this.cut.offsetWidth;
this.para.cutHeight=this.cut.offsetHeight;
this.addEvent(document,"mousemove",this.bindEvent(this,this.RightDownMouseMove));
this.addEvent(document,"mouseup",this.bindEvent(this,this.RightDownMouseUp));
// var handler = this;
// document.onmousemove=function(e){
// e=e||window.event;
// handler.RightDownMouseMove(e);
// }
// document.onmouseup=function(){
// document.onmousemove=null;
// document.onmouseup=null;
// }
},
RightDownMouseMove:function(){
var e=arguments[0]
e.stopPropagation ? e.stopPropagation() : (e.cancelBubble = true);
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
this.cut.style.width=e.clientX-this.para.mouseX+this.para.cutWidth+"px";
this.cut.style.height=e.clientY-this.para.mouseY+this.para.cutHeight+"px";
var rectTop=(parseInt(this.cut.style.top)+1)+"px";//加1边框
var rectRight=(parseInt(this.cut.style.left)+parseInt(this.cut.style.width)+1)+"px";
var rectBottom=(parseInt(this.cut.style.top)+parseInt(this.cut.style.height)+1)+"px";
var rectLeft=(parseInt(this.cut.style.left))+"px";
this.cutImg.style.clip="rect("+rectTop+" "+rectRight+" "+rectBottom+" "+rectLeft+")";
},
RightDownMouseUp:function(){
this.deleteEvent(document,"mousemove",this.RightDownMouseMove);
this.deleteEvent(document,"mouseup",this.RightDownMouseUp);
},
bindEvent:function(obj,fun){
return function(e){
return fun.apply(obj,[e||window.event]);
}
},
addEvent:function(oTarget,name,funName){
if(oTarget.addEventListener) {
oTarget.addEventListener(name,funName,false);
}
else if(oTarget.attachEvent) {
oTarget.attachEvent("on" +name,funName);
}
else{
oTarget["on" + name] = funName;
}
},
deleteEvent:function(oTarget,name,funName){
if(oTarget.removeEventListener) {
oTarget.removeEventListener(name,funName,false);
}
else if(oTarget.detachEvent) {
oTarget.detachEvent("on" + name,funName);
}
else{
oTarget["on" + name] = null;
}
}
}
lsw645645645 2011-07-09
  • 打赏
  • 举报
回复
学习,我也想知道
huangwenquan123 2011-07-09
  • 打赏
  • 举报
回复
js版的大牛啊,周末都去玩了吗?

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧