Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple assignments var a = b = b || {} in javascript

Reading leaflet.js 's code, I came across a method with this line which I don't fully understand:

var events = this._leaflet_events = this._leaflet_events || {};

which can be simplified as

var a = b = b || {};

From what I understand this instruction is a multiple left-handed assignment that is right associative which means that first, JavaScript will run

b = b || {} //if b exists, returns b, else return an empty object

, then

a = b // returns the output of the preceding instruction

Which does not make sense to me. Why not write instead:

a = b || {};

Full context:

addEventListener: function( /*string*/ type, /*function */ fn, /*(optional) object*/ context){
    var events = this._leaflet_events = this._leaflet_events || {};
        events[type] = events[type] || {};
        events[type].push({
        action: fn,
        context: context || this
        });
    return this;
}

I suspect a reference trick since I don't see how this._leaflet_events gets modified by the method otherwise.


Thinking about it, writing var a = b = b || {} is actually be a trick to assign var a a reference to b, no matter whether b is defined or not. Modifying a now modifies b.

Back to Leaflet. With

    var events = this._leaflet_events = this._leaflet_events || {};

this._leaflet_events either exists or is initialized to {}. events is assigned this._leaflet_events by reference. The reference's value might be {} but it is still this._leaflet_events that is being modified when modifying events.

On the contrary, writing

    var events = this._leaflet_events || {};

would be a mistake , since if this._leaflet_events is not defined, events will now point to a newly created object whose value will be {}. Modifying events will change the new object but it won't change this._leaflet_events's value.

Same appearent values, different references. Here is the thing.

like image 863
cedricbellet Avatar asked May 28 '12 22:05

cedricbellet


People also ask

How do you do multiple assignments in JavaScript?

“JavaScript allows multiple left-hand assignments” var a = b = c = d = 10; The assignment expressions are always evaluated from right-to-left. So what the above expression actually does is, assign the value 10 to the variable d , then assign the value of d to c and so on.

What does var {} mean in JavaScript?

The var statement declares a variable.

Can you assign multiple variables in JavaScript?

You can set multiple variables to the same value in JavaScript by using the equal sign (=) consecutively between the variable names and assigning a single value at the end when declaring the variables. var c = 10; var b = c; var a = b; You can also declare the variables first and assign the value later.

Can a variable hold multiple values in JavaScript?

There is no way to assign multiple distinct values to a single variable. An alternative is to have variable be an Array , and you can check to see if enteredval is in the array. To modify arrays after you have instantiated them, take a look at push , pop , shift , and unshift for adding/removing values.


2 Answers

The statement var a = b = b || {}; does two things:

  • It initializes b to {} if it was undefined.
  • It sets a to the same as b.

The expression a = b || {}; does not not modify b so it is not equivalent.

like image 63
Mark Byers Avatar answered Sep 25 '22 00:09

Mark Byers


The shorter expression will not set anything as b's value

a = b = b || {}; //set b's b value to {} if b is uncdefined, then set a's value to b

a = b || {}; //set a's value to b, or {} if b is undefined

the first statement is in fact equivalent to

b = b || {};
a = b;
like image 36
wheresrhys Avatar answered Sep 22 '22 00:09

wheresrhys