While reading through the annotated Backbone source code, I encountered an assignment like this:
var Events = Backbone.Events = {};
I understand what is happening: An empty object is assigned to Backbone.Events
, which is then assigned to Events
.
What is the reason the assignment is written like this?
In general I know that multiple left hand assignments are a bad idea because if done like this:
var a = b = c = 1;
b
and c
are hoisted to the global namespace, but in the case of backbone, Events
is a local variable and Backbone.Events
is bound to the Backbone
object, so no scope pollution here – is this correct?
So is it written like this so obtain a 'copy' of Backbone.Events
in Events
because objects are always passed by reference in JavaScript? This would mean changes to Events
would also apply to Backbone.Events
because it's actually the same object, but I don't see any advantage in this, but maybe I miss something here? Maybe it's just really a short way for this?
var Events = {};
var Backbone.Events = {};
Edit: The above example is actually wrong, I just realized this because of Guffa's answer.