![](/img/trans.png)
[英]How can I combine that javascript and jQuery function to hide a button?
[英]How can I combine function calls in JavaScript?
我目前正在開發一個 WebSocket。 為此,我創建了一個 JavaScript 類。 在這個類中,我有 2 個方法: subscribe()
和bind()
。 第一種方法訂閱一個頻道,下一個方法應該收聽它。
但是當我這樣調用我的方法時:
let socket = new CustomWebSocket();
socket.subscribe("my-channel").bind("my-event", function (response) {
console.log(response);
});
我在控制台中收到一條錯誤消息:
Uncaught TypeError: socket.subscribe(...).bind is not a function
這就是我在CustomWebSocket
類中編寫函數的方式:
subscribe(channelName) {
let self = this;
let subMsg = {
type: "subscribe",
channel: channelName
};
self.ws.send(JSON.stringify(subMsg));
return channelName;
}
bind(eventName, callback) {
let self = this;
self.ws.onmessage = function (event) {
let eventData = JSON.parse(event.data);
if (eventData.type === "channelMessage") {
callback(eventData.payload);
}
}
}
我做錯了什么? 它認為它可以這樣工作......
您正在返回channelName;
從subscribe
。 因此,您實際上是在嘗試在channelName
上調用bind
。
為了能夠對subscribe
的返回值調用bind
,您需要從subscribe
返回self
或this
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.