我有一个 javascript 对象,我希望它能够处理一些交互功能。以简单的方式描述场景有点棘手,所以希望它不会在这里完全失控。
我的对象看起来像
myobject = function() {
this.initialize = function() {
// HERE this = the myobject instance
var test = document.createElement('div');
test.onmousedown = this.mousedown;
}
this.mousedown = function(e) {
// HERE this = the calling div-element
}
}
所以我的问题基本上是在被调用时this
不会是myobject
实例this.mousedown(e)
,而是调用者(如果术语正确?)在这种情况下,它是我创建的 div 并放入test
上面调用的变量中。
我想访问正在运行该方法的实例(因为我相信这是mousedown
我创建的方法)。
到目前为止,我已经尝试了一些想法:
- 在包含对象的对象上创建一个
data-
属性并对其进行操作。div
this
- 将 this 指针作为参数与
e
to一起发送this.mousedown(e)
我现在能想到的就这些了,希望有道理。