1

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.

Sven
  • 12,997
  • 27
  • 90
  • 148
  • "... because objects are always passed by reference in JavaScript?". Yes. The author also defines an `Events` object so the source code becomes cleaner. – Ram Oct 14 '15 at 20:32
  • @Vohuman With cleaner you mean that you just have to type `Events` instead of `Backbone.Events`? – Sven Oct 14 '15 at 20:34
  • Yes. Is it good or bad? The answer is "primarily opinion-based". – Ram Oct 14 '15 at 20:36
  • *"because objects are always passed by reference"* JavaScript is always *pass by value*. Objects are *represented as* references, so in this case the variable is assigned **a** references, not **by** reference. This is a big difference that unfortunately too many people get wrong. Pass-by-reference means I can do this: `var a = 1; var b = a; a = 2;` and `b` would be `2` as well. But that's not how JavaScript works (for any value / data type). For more information see https://en.wikipedia.org/wiki/Evaluation_strategy . – Felix Kling Oct 14 '15 at 20:41
  • @FelixKling Thank you, I've indeed never heard of that! Do you have any source where to find correct information for stuff like this? – Sven Oct 14 '15 at 20:47
  • Other than the Wikipedia article and the linked resources there, no. – Felix Kling Oct 14 '15 at 20:48

2 Answers2

1

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 is exactly what it is. That simply means that by having an event at the backbone you can then call it as a global variable. For example if you do this:

 Backbone.Events.testEvent = "just a quick proof of concept test";

You can see that this also exist at the Events variable and via versa.

Updating answer

From the documentation backbone is actually using this kind of variables for a quick reference to the events or other resources from Backbone.

I think it is a bad practice however to use multiple assignments into one line.

Community
  • 1
  • 1
mikelamar
  • 178
  • 1
  • 6
  • 1
    "It is more preferable to not use global variables to your project for "quick" access". The code doesn't define a _global_ variable here. – Ram Oct 14 '15 at 20:33
1

Yes, the Events variable will point to the same object as the Backbone.Events property. It's the short form of:

Backbone.Events = {};
var Events = Backbone.Events;
Guffa
  • 687,336
  • 108
  • 737
  • 1,005