我有以下情况。在伪类的构造函数中,我将一个单击事件附加到一个元素。当事件被触发时,我想从回调函数中引用设置事件的对象。从jQuery中的回调函数引用对象
码伪类的构造函数
function MyClass(){
this.myClassAttribute = "A class attribute";
// here `this` refers to the object
$("span").click(function(){
// here `this` refer to a matched element, i.e. "span"
// How to get the value of `myClassAttribute`?
});
}
如何引用对象没有全局变量?
您的点击处理函数是一个闭包,它们是javascript http://www.google.com/search?q=javascript+closures的强大部分 – JeremyWeir 2009-10-16 18:46:38
我知道这已经有几年了。但是我遇到了这个解决方案的问题。如果你有几个类实例,最好使用@Stephen Delano提供的答案。因为myClass将始终包含已创建的最后一个实例。 – Jodo 2015-11-27 14:55:47