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一员,即使在对象改变。
这似乎并不工作:
// 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)},
我想明白为什么这两种方法有根本的不同。
无论如何,它似乎是由我工作,因为JS不会破坏功能,直到至少有一个指针。我可以看到你的setCallback函数吗? – birukaze 2011-01-26 10:32:15