我正在使用jQuery,并尝试将一些基本的Javascript面向对象编程原则应用于控制悬停行为的一组函数。然而,我无法弄清如何让"this"关键字引用我创建的对象实例。我的示例代码如下:
var zoomin = new Object();
zoomin = function() {
// Constructor goes here
};
zoomin.prototype = {
hoverOn: function() {
this.hoverReset();
// More logic here using jQuery's $(this)...
},
hoverReset: function() {
// Some logic here.
}
};
// Create new instance of zoomin and apply event handler to matching classes.
var my_zoomin = new zoomin();
$(".some_class").hover(my_zoomin.hoverOn, function() { return null; });
在上述代码中有问题的一行是在
hoverOn()
函数内调用this.hoverReset()
。由于 this
现在指的是鼠标悬停的元素,所以它无法按照预期工作。我基本上想要为对象的该实例(my_zoomin
)调用函数 hoverReset()
。有没有任何方法可以做到这一点?