通过回调将参数传递给第三方函数

时间:2018-06-09 07:38:31

标签: javascript callback parameter-passing

我正在尝试执行以下基于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");

我认为这会模拟启动回调的第三方函数,作为将类实例的知识传递给回调的一种方法(因为触发回调的函数会改变'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.
        }

    });
}

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

1 个答案:

答案 0 :(得分:0)

使用相同的方法找到问题的答案。 socketCallback需要在类中。一旦它

this.stream_this

可以访问类实例。