如何访问类原型Javascript中定义的事件处理程序中的类成员变量
How to access class member variables inside an event handler defined in a class prototype - Javascript
我见过这样的解决方案,当在构造函数中定义事件时,它可以工作:
在Javascript 中访问事件处理程序内的类成员变量
但是,当在原型中定义处理程序时,它没有查看以前解决方案中的"var_self"的范围。是否有一种变体适用于下面类似的代码?
问题是"幻灯片"处理程序中的"this"。。。引用HTML元素而不是MyView中的成员变量。
function MyView(wrapperDiv)
{
this.contentW = 1200;
this.vboxW = 600;
this.vboxH = 400;
this.vboxX = 0;
this.vboxY = 0;
}
MyView.prototype = {
initHorizontalScrollBar : function ($bar)
{
//create the wrappers for the slider
if($bar.find('.slider-wrap').length==0)
$bar.append('<'div class="slider-wrap"><'div class="slider-horizontal"><'/div><'/div>');//append the necessary divs so they're only there if needed
$bar.find('.slider-wrap').width(this.svgWidth);
//initialize the JQueryUI slider
$bar.find('.slider-horizontal').slider({
orientation: 'horizontal',
min: 0,
max: 100,
range:'min',
value: 0,
slide: function(event, ui) {
var difference = this.contentW-this.vboxW;
if(difference<=0)
return;
this.vboxX = (ui.value) / 100 * ( this.contentW-this.vboxW); // 0 >= ui.value <= 100
this.svg.setAttribute("viewBox", toViewBoxString(this.vboxX, this.vboxY, this.vboxW, this.vboxH));
$('ui-slider-range').width(ui.value+'%');//set the height of the range element
}
});
this.sizeHScrollbar();
}
};
您只需要在initHorizontalScrollBar方法中添加一个包含正确引用的变量:
MyView.prototype = {
initHorizontalScrollBar : function ($bar)
{
var _self = this;
//create the wrappers for the slider
if($bar.find('.slider-wrap').length==0)
$bar.append('<'div class="slider-wrap"><'div class="slider-horizontal"><'/div><'/div>');//append the necessary divs so they're only there if needed
$bar.find('.slider-wrap').width(this.svgWidth);
//initialize the JQueryUI slider
$bar.find('.slider-horizontal').slider({
orientation: 'horizontal',
min: 0,
max: 100,
range:'min',
value: 0,
slide: function(event, ui) {
var difference = _self.contentW - _self.vboxW;
if(difference<=0)
return;
_self.vboxX = (ui.value) / 100 * ( _self.contentW - _self.vboxW); // 0 >= ui.value <= 100
this.svg.setAttribute("viewBox", toViewBoxString(_self.vboxX, _self.vboxY, _self.vboxW, _self.vboxH));
$('ui-slider-range').width(ui.value+'%');//set the height of the range element
}
});
this.sizeHScrollbar();
}
};
相关文章:
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- 没有在Angular应用程序中定义firebase(在firebase迁移之后)
- Highcharts-如何在自定义格式化程序中获得默认的y轴标签格式化程序
- 为什么这个函数调用会破坏程序并导致未定义的变量
- 如何在ExtJs4应用程序上为视图、存储和模型设置自定义文件夹名称
- 制作自定义铬应用程序顶部栏
- 使用javascript为web应用程序自定义键盘快捷键
- Dojo AMD加载程序执行定义具有空/缺失依赖项的回调
- 使用自定义数据属性或将数据绑定到处理程序来处理事件
- 如何在Sencha Touch应用程序中实现自定义Toast
- importScripts在web工作程序中返回未定义的
- 注入应用程序的角度控制器未定义
- 为什么我的程序提醒未定义的值
- Ionic应用程序在部署后给出ReferenceError:未定义Promise
- 运行此程序后出现错误,未捕获的类型错误:无法读取未定义的属性“推送”
- 如何使用ngModel将自定义控件的提供程序扩展器分离到Angular 2中的单独文件中
- 设计颜色自定义程序
- Wordpress自定义程序具有页面编辑功能
- 在Javascript中使用Wordpress自定义程序
- 如何使用自定义程序克隆DeepWith_并添加属性