[英]If I assign a new value to JS variable does it break its bindings?
I have a javascript variable that stores a websocket connection like so: 我有一个JavaScript变量,可以像这样存储websocket连接:
var ws = $.websocketSettings.factory(url);
$(ws)
.bind('open', $.websocketSettings.open)
.bind('close', $.websocketSettings.close)
.bind('message', $.websocketSettings.message)
.bind('error', $.websocketSettings.error)
.bind('relaySent', $.websocketSettings.relayTimeout)
.bind('relayRetry', $.websocketSettings.relayRetry)
.bind('jumpStart', $.websocketSettings.jumpFactory);
However in case the connection drops, I have a jumpStart
event that resets the ws
variable with a new connection. 但是,如果连接断开,我将有一个
jumpStart
事件,该事件将使用新连接重置ws
变量。
jumpFactory: function(event){
var ws = this;
ws = $.websocketSettings.factory( getEngineUrl() );
return ws;
},
Will this break the binded events on ws
? 这会破坏
ws
上的绑定事件吗? I'm seeing some strange behaviour so trying to pinpoint the cause. 我看到一些奇怪的行为,因此试图查明原因。 Thanks.
谢谢。
You don't bind your events to any variable, but to its value - object referenced in it. 您不会将事件绑定到任何变量,而是绑定到其值(其中引用了对象)。 Assigning new object to variable won't somehow automagically transfer those bindings from old object to new.
将新对象分配给变量不会以某种方式自动将那些绑定从旧对象转移到新对象。 Additionally those events will continue to fire as long as this object old object is referenced from somewhere.
另外,只要从某个地方引用了该旧对象,这些事件将继续触发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.