简体   繁体   English

如果我为JS变量分配新值,是否会破坏其绑定?

[英]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.

相关问题 将值分配给新变量? - assign value to new variable? 为$ _GET变量分配一个新值 - assign a new value to a $_GET variable 为什么不能在块内为变量分配新值? - Why can't I assign new value to variable inside a block? 将输入值分配给JS变量 - Assign input value to JS variable 在具有声明的空变量的函数中,switch语句如何自动将其返回值分配给空变量? - In a function with a declared empty variable, how does a switch statement automatically assign its return value to the empty variable? 我可以从另一个变量分配一个变量而不改变它在 JavaScript 中的值吗? - Can I assign a variable from another variable without changing its value in JavaScript? 将JS变量值分配给剃刀变量 - assign JS variable value to razor variable 如何根据 select 标签的选定选项的值为变量分配新值,检索并使用变量的新值? - How do I assign a new value to a variable based on the value of a selected option of a select tag, retrieve and use the new value of the variable? 使用自身为变量分配新值 - Assign new value to variable using itself 当我将$ scope分配给另一个变量并更改新变量的监视值时,不会触发Angular $ scope。$ watch - Angular $scope.$watch is not triggered when i assign $scope to another variable and change the watched value on new variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM