回调动态对象
this.a = new A()
this.b = new B()
// point b callback to a member of object a
this.b.setCallback(this.a.mycallback)
// initiate callback
this.b.doCallback() // calls this.a->mycallback()
// ..... later on
// replace this.a with a new object
this.a = new A()
// callback is broken now?
this.b.doCallback()
如何让this.b的回调方法指向this.a的成员,无论this.a是否被替换为新实例?换句话说,它是动态解析的吗?
我知道如何用其他语言执行此操作,但由于某种原因,在 Javascript 中我似乎无法弄清楚。
[编辑]
问题似乎有所不同。回调函数已在对象的原型中声明。但现在我已经隔离了我认为的问题。
下面您可以看到我的对象的一段代码。它包含2个成员对象。一名播放器和一种波形。播放器是在新的 url 可用后动态实例化的东西。我希望波形对 this.player 的成员进行名为“seek”的回调,即使在对象发生更改后也是如此。
这似乎不起作用:
// placeholder, object is instantiated later
this.player = {}
var self = this
this.waveform = new Waveform(this.waveformDiv, {
seek : self.player.seek,
})
// after this point this.player can be instantiated multiple times with
// different Player objects. I want the callback to seek to point to the new instance.
... 但一旦我使用匿名函数设置了搜索选项,它就会起作用:
seek : function(time){ self.player.seek(time)},
我想了解为什么这两种方法有根本的不同。
this.a = new A()
this.b = new B()
// point b callback to a member of object a
this.b.setCallback(this.a.mycallback)
// initiate callback
this.b.doCallback() // calls this.a->mycallback()
// ..... later on
// replace this.a with a new object
this.a = new A()
// callback is broken now?
this.b.doCallback()
How can I make the callback method of this.b point to the member of this.a regardless whether this.a is replaced with a new instance? In other words have it resolved dynamically?
I know how to do this in other languages but for some reason in Javascript I can not seem to figure it out.
[edit]
The problem seems to be something different. The callback functions were already declared in the prototype of the object. But now I've isolated the problem I think.
Below you see a piece of code from my object. It contains 2 member objects. One player and one waveform. The player is something which is instantiated dynamically after a new url is available. I want the waveform to make a callback, named "seek", to a member of this.player, even after the object changed.
This doesn't seem to work:
// placeholder, object is instantiated later
this.player = {}
var self = this
this.waveform = new Waveform(this.waveformDiv, {
seek : self.player.seek,
})
// after this point this.player can be instantiated multiple times with
// different Player objects. I want the callback to seek to point to the new instance.
...
but it DOES work once I set the seek option using an anonymous function:
seek : function(time){ self.player.seek(time)},
I would like to understand why the 2 approaches are fundamentally different.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 mycallback 添加到 A 的原型中。
A.prototype.mycallback = function (...) {...};
此后 A 的任何实例都会有 mycallback。
Add mycallback to prototype of A.
A.prototype.mycallback = function (...) {...};
After this any instance of A will have mycallback.
我想我现在可以回答我自己的问题了。
这将创建对玩家对象方法seek 的直接引用。因为它是对特定实例方法的引用,所以一旦玩家被替换为不同的实例,该方法就会中断。内存中的播放器位置发生变化,其成员函数位置也会发生变化。
这次创建了一个函数并且seek 保存了对该函数的引用。这不直接链接到播放器实例。当调用 seek 时,将执行该函数,并在每次调用时评估函数中的变量。因此,当函数执行 self.player 时,会查找并指向正确/新的玩家对象。
I think I can answer my own question now.
This creates a direct reference to the player object method seek. Because it is a reference to the specific instance method this breaks as soon as player is replaced with a different instance. The player location in memory changes and so will its member function locations.
This time a function is created and seek holds a reference to the function. This is not linked to the player instance directly. When seek is called, the function is executed and the variables in the function are evaluated on every call. So when the function executes self.player is looked up and will point to the correct/new player object.