简体   繁体   English

回调到动态对象

[英]Callback to dynamic objects

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? 我如何使this.b的回调方法指向this.a的成员,而不管this.a是否被新实例替换? 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. 我知道如何用其他语言执行此操作,但是由于某种原因,在Javascript中,我似乎无法弄清楚。

[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. 它包含2个成员对象。 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.player的成员进行名为“ seek”的回调。

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. 我想理解为什么两种方法根本不同。

Add mycallback to prototype of A. 将mycallback添加到A的原型。

A.prototype.mycallback = function (...) {...}; A.prototype.mycallback =函数(...){...};

After this any instance of A will have mycallback. 此后,A的任何实例都将具有mycallback。

I think I can answer my own question now. 我想我现在可以回答我自己的问题。

seek : self.player.seek 

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. 播放器在内存中的位置会更改,其成员功能位置也会更改。

seek : function(time){ self.player.seek(time)}

This time a function is created and seek holds a reference to the function. 这次创建了一个函数,并且seek拥有对该函数的引用。 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. 调用seek时,将执行该函数,并在每次调用时评估函数中的变量。 So when the function executes self.player is looked up and will point to the correct/new player object. 因此,当函数执行self.player时,它将查找并指向正确/新的播放器对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM