如何在pubnub订阅/发布函数中获取channelid?

这我的订阅代码我想要得到的渠道,所以我在代码中使用this.channel ,但我得到了undefined。 有没有什么办法可以得到channelid

pubnub.subscribe({ channel: changing dynamically, presence: function (m) { console.log(m) }, message: function (m) { console.log(m); console.log("Channel ==" + this.channel) }, error: function (error) { // Handle error here console.log(JSON.stringify(error)); } }) 

结果:Channel ==未定义

看一下精细的手册 ,这应该工作:

 pubnub.subscribe({ channel: changing dynamically, presence: function (m) { console.log(m) }, message: function (m, env, channel) { console.log(m); console.log("Channel ==" + channel) }, error: function (error) { // Handle error here console.log(JSON.stringify(error)); } }) 

换句话说,通道作为parameter passing给messagecallback。

未定义this.channel的最可能原因是messagecallback在传递给pubnub.subscribe()的对象的上下文中不被调用。