$.proxy()没有按预期工作.我怀疑它对嵌套对象不起作用
$.proxy() is not working as expected. I suspect it doesn't work on nested object
我第一次了解这个函数是通过阅读bootstrap.js源代码。如果你不熟悉,这是文件。
通过多次试验&错误,我发现$.proxy()不适用于多层对象。这是真的吗?下面是我的代码:
ProgressBtn.prototype.reset = function(ms) {
var self = this;
//this.$progressInicator is a jquery object
// here is the problem: reset() call is not working, nor displaying error
var reset = $.proxy( this.$progressIndicator.css,
this,
{'background-color': 'red'}
);
reset();
console.log('reset is running');
}
我认为这行代码有问题:
var reset = $.proxy( this.$progressIndicator.css,
this,
{'background-color': 'red'}
);
你能帮我诊断这个问题吗?谢谢!
问题是你有这个:
var reset = $.proxy( this.$progressIndicator.css,
this,
{'background-color': 'red'}
);
这基本上会导致这种情况发生:
this.$progressIndicator.css.call(this, {'background-color': 'red'});
但是this
不是一个jQuery对象,用这个上下文调用css
将不起作用。
你需要这个:
var reset = $.proxy( this.$progressIndicator.css,
this.$progressIndicator,
{'background-color': 'red'}
);
然而,我不明白为什么你这样做,如果你想要你的reset
方法做的唯一的事情是改变一个对象的CSS属性。您可以使用以下命令实现相同的目的:
ProgressBtn.prototype.reset = function(ms) {
this.$progressIndicator.css({'background-color': 'red'});
};
$.proxy
是有用的,当你想要附加一个处理程序到一个事件,并希望该处理程序执行与特定的上下文(如。net委托)
相关文章:
- 用嵌套函数和默认函数定义函数
- 如何在ReactJS JSX中执行嵌套的if-else语句
- 可以简化嵌套的延迟Q Promises解析吗
- 用于搜索的聚合物嵌套绑定
- 如何使用javascript获取嵌套对象中所有子对象的单个属性
- d3中堆栈函数和嵌套函数之间的差异
- 如何打印嵌套对象的所有值
- JavaScript 中的嵌套函数和 “this” 关键字
- 设置嵌套对象属性的更好方法
- querySelector/getElementByClassName嵌套项的顺序
- 猫鼬在特定记录中查找嵌套记录
- 访问嵌套JSON对象的键,其中键是动态的
- D3嵌套组作为x轴
- Ionic和angularjs嵌套步骤应用程序
- 从多维嵌套json数组创建下拉列表
- 默认情况下折叠和展开嵌套列表
- 嵌套对象结构
- 显示嵌套窗体
- 带嵌套json的下划线js查找
- $.proxy()没有按预期工作.我怀疑它对嵌套对象不起作用