首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过带有回调的第三方函数将参数传递到

通过带有回调的第三方函数将参数传递到
EN

Stack Overflow用户
提问于 2018-06-09 15:38:31
回答 1查看 150关注 0票数 0

我正在尝试基于mastering-javascript-callbacks-bind-apply-call做一些类似以下示例的事情。我有一个管理回调的类,它被触发了,我想再次从类内部触发一些代码。在示例中,我在定义回调时将一个额外的变量绑定到类'this‘中,这样当回调被触发时,它仍然可以从类中调用函数。所有这些都运行得很好。

代码语言:javascript
复制
    #!/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‘不见了。

代码语言:javascript
复制
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.
        }

    });
}

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

EN

回答 1

Stack Overflow用户

发布于 2018-06-09 16:50:17

用同样的方法找到了问题的答案。socketCallback需要位于类内部。曾经是

代码语言:javascript
复制
this.stream_this

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50772005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档