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.
“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.
The var statement declares a variable.
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.
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.
The statement var a = b = b || {};
does two things:
{}
if it was undefined.The expression a = b || {};
does not not modify b so it is not equivalent.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With