我正在尝试基于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.
}
});
}如何将知识传递给类实例的回调,以便从回调中访问类实例的变量和函数?我知道我可以通过删除类并为我想要存储的数据设置一个全局变量来做到这一点,但是对于我可能并行管理的多个流实例,代码是不可重入的。
发布于 2018-06-09 16:50:17
用同样的方法找到了问题的答案。socketCallback需要位于类内部。曾经是
this.stream_this工作,并且可以访问类实例。
https://stackoverflow.com/questions/50772005
复制相似问题