Javascript作用域丢失问题
这是我的下拉菜单课。在hide方法中,调用函数afterFinsih 放宽类的范围。我不想为此声明一个全局变量。有没有更好的解决办法Javascript作用域丢失问题,javascript,prototypejs,Javascript,Prototypejs,这是我的下拉菜单课。在hide方法中,调用函数afterFinsih 放宽类的范围。我不想为此声明一个全局变量。有没有更好的解决办法 var DropMenu = Class.create({ // Class properties. speed: 0.4, delay: 0, active_submenu: null, initialize: function() { $$("li.drop_down_element").invoke
var DropMenu = Class.create({
// Class properties.
speed: 0.4,
delay: 0,
active_submenu: null,
initialize: function() {
$$("li.drop_down_element").invoke('observe', 'mouseenter', this.mouseOver.bind(this));
$$("li.drop_down_element").invoke('observe', 'mouseleave', this.mouseOut.bind(this));
},
show: function(elem, speed) {
new Effect.BlindDown(elem, {duration: speed, queue: {position: 'end', scope: elem.identify(), limit:13}});
},
hide: function(elem, speed) {
if(elem.visible())
{
new Effect.BlindUp(elem, {duration: speed, queue: {position: 'end', scope: elem.identify(), limit: 13}, afterFinish: function(scope) { console.info(scope)}(this)});
}
},
mouseOver: function(event) {
var element = event.element(),
submenu = element.getElementsByClassName('submenu_element')[0],
width = element.getWidth();
if(submenu)
{
submenu.setStyle({minWidth: width + 'px'})
this.active_submenu = submenu;
this.show(submenu, this.speed);
}
if(!this.iefix && Prototype.Browser.IE)
{
new Insertion.After(submenu,
'<iframe id="dropdown_iefix" '+
'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' +
'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
this.iefix = $(submenu.id+'_iefix');
}
//if(this.iefix) setTimeout(this.fixIEOverlapping.bind(this), 50);
},
mouseOut: function(event) {
var submenu = this.active_submenu;
if (submenu)
{
this.hide.delay(this.delay, submenu, this.speed);
}
},
fixIEOverlapping: function() {
Position.clone(this.active_submenu, this.iefix, {setTop:(!this.active_submenu.style.height)});
this.iefix.style.zIndex = 700;
this.iefix.style.border = '2px solid #000';
this.active_submenu.style.zIndex = 999;
Element.show(this.iefix);
}
});
document.observe('dom:loaded', function() { new DropMenu(); });
var DropMenu=Class.create({
//类属性。
速度:0.4,
延迟:0,
活动子菜单:空,
初始化:函数(){
$$(“li.drop\u down\u元素”).invoke('observe','mouseenter',this.mouseOver.bind(this));
$$(“li.drop\u down\u元素”).invoke('observe','mouseleave',this.mouseOut.bind(this));
},
显示:功能(要素、速度){
新的Effect.BlindDown(elem,{duration:speed,queue:{position:'end',scope:elem.identify(),limit:13});
},
隐藏:功能(元素、速度){
if(elem.visible())
{
新效果.BlindUp(elem,{duration:speed,queue:{position:'end',scope:elem.identify(),limit:13},afterFinish:function(scope){console.info(scope)}(this)});
}
},
鼠标悬停:函数(事件){
var element=event.element(),
submenu=element.getElementsByClassName('submenu\u element')[0],
width=element.getWidth();
如果(子菜单)
{
子菜单.setStyle({minWidth:width+'px'})
this.active_子菜单=子菜单;
this.show(子菜单,this.speed);
}
if(!this.iefix&&Prototype.Browser.IE)
{
新插入。之后(子菜单,
'');
this.iefix=$(子菜单.id+“u iefix”);
}
//if(this.iefix)setTimeout(this.fixeoverlapping.bind(this),50);
},
mouseOut:函数(事件){
var子菜单=此.active_子菜单;
如果(子菜单)
{
this.hide.delay(this.delay,子菜单,this.speed);
}
},
FixeOverlapping:function(){
clone(this.active_子菜单,this.iefix,{setTop:(!this.active_子菜单.style.height)});
this.iefix.style.zIndex=700;
this.iefix.style.border='2px solid#000';
this.active_子菜单.style.zIndex=999;
元素.show(this.iefix);
}
});
observe('dom:loaded',function(){newdropmenu();});
您可以执行以下操作:
if(elem.visible())
{
var that = this;
new Effect.BlindUp(elem, {
duration: speed,
queue: {position: 'end', scope: elem.identify(), limit: 13},
afterFinish: function() { console.info(that); }
});
}
。。。但你可能不喜欢这样。某些浏览器支持Function.prototype.bind
,允许您创建绑定到所需范围的函数,例如:
if(elem.visible())
{
var that = this;
new Effect.BlindUp(elem, {
duration: speed,
queue: {position: 'end', scope: elem.identify(), limit: 13},
afterFinish: (function() { console.info(this); }).bind(this);
});
}
如果您没有
Function.prototype.bind
,可以使用此代码段()来定义它(您声明您正在使用prototype,prototype应该已经有了它):
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") // closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be fBound is not callable");
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP ? this : oThis || window, aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}