【问题标题】:Passing a parameter to through a 3rd party function with a callback通过带有回调的 3rd 方函数传递参数
【发布时间】:2018-11-19 04:27:13
【问题描述】:

我正在尝试基于mastering-javascript-callbacks-bind-apply-call 执行以下示例。我有一个管理回调的类,它被触发,我想再次从类内部触发一些代码。在示例中,我在定义回调时将一个额外的变量绑定到类“this”中,以便在触发回调时仍然可以从类中调用函数。所有这些都可以正常工作。

    #!/usr/bin/env node

class Doge {
    constructor(callback) {
        // bind an extra variable to the callback that represent the class instance 'this' context
        let boundCallback = callback.bind({doge: this});

        // associate the callback with the class
        this.callback = boundCallback;
    }

    // a way to initiate the callback (would normally be triggered with getting data for instance)
    doCallback(data) {
        this.callback(data);    
    }

    // something inside the class we want to get
    getSaying() {
        return "Such callback!";    
    }
}

//callback normally only gets one variable (i.e. the incoming data), but we've been sneaky and associated some data
// in the this context ...which was the this from the class instance
function callback(data) {
    console.log(data);
    console.log(this.doge.getSaying()); 
}

// creates class instance andsets the callback function (which is not in the class)
var doge = new Doge(callback); 

// call the callback with some data
doge.doCallback("test");

我认为这会模拟一个启动回调的第 3 方函数,以此将类实例的知识传递给回调(因为触发回调的函数会更改“this”上下文),但我错了或犯了一个错误。

我的真实示例使用 Web 套接字库 (ws)。我试图实现一个类似的概念,但它抱怨因为我绑定到 socketCallback 的 'stream_this' 不见了。

const WebSocket = require('ws');

module.exports = class Stream {

    constructor(socket_url) {
        //we want to store data unique to the class instance here
        this.stream_data = {}
        this.ws = new WebSocket(socket_url);

        let boundCallback = socketCallback.bind({stream_this: this});
        this.callback = boundCallback;

        this.ws.on('message', boundCallback); // <--- callback association
    }

    addChannels(channels) {
        var self = this;

        this.ws.on('open', function open() {    
            self.ws.send(JSON.stringify(channels, null, 4));
        });           
    }

}

function socketCallback(data) {
    var self = this;
    var response = JSON.parse(data);  // turn the string back to JSON

    // primative response processor
    response.forEach(function(result) {

        if (result.channel == "addChannel") {
            // confirm the channel is subscribed too     
        } else {
            // associate data with the class instance somehow
            this.stream_this.stream_data[result.timestamp] = result.data  <- this.stream is undefined.
        }

    });
}

如何将知识传递给类实例的回调,以便我可以从回调中访问类实例的变量和函数?我知道我可以通过删除类并为我想要存储的数据设置一个全局变量来做到这一点,但是对于我可能并行管理的多个流实例,代码不会重入。

【问题讨论】:

    标签: javascript callback parameter-passing


    【解决方案1】:

    用同样的方法找到了问题的答案。 socketCallback 需要在类中。曾经是

    this.stream_this
    

    工作并且可以访问类实例。

    【讨论】:

      猜你喜欢
      • 2015-08-29
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多