Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8010361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:42:51+00:00 2026-06-04T18:42:51+00:00

Reading leaflet.js ‘s code, I came across a method with this line which I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:42:53+00:00Added an answer on June 4, 2026 at 6:42 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading the design guidelines. I came across a little issue while trying to practice
Reading code from other posts, I'm seeing something like this. struct Foo { Foo()
Reading this site, I've found this : [The] line private static final Foo INSTANCE
Reading through wikipedia I came across the concept of Sprints in Agile development. From
Reading through this , I came to the bit on default values for function
Reading this question I found this as (note the quotation marks) code to solve
Reading on another forum I've came across the world of CSS Frameworks. The one
Reading for hours, I am pretty sure I understand how blocks in Jade work.
Reading the code of many javascript libraries, I see that many developers use to
Reading source code of my current project, I see: [self retain] in one class,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.