Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "this._events || (this._events = {});" mean?

I have started learning Backbone.js. Currently my JavaScript skills are not too good. I have started to examine the backbone.js file and have come across a strange line of code whose purpose I can not figure out. Code sample (if you need more context, manually download backbone.js for developers and see line 80):

var Events = Backbone.Events = {

  // Bind an event to a `callback` function. Passing `"all"` will bind
  // the callback to all events fired.
  on: function(name, callback, context) {
    if (!eventsApi(this, 'on', name, [callback, context]) || !callback) return this;
    this._events || (this._events = {});
    var events = this._events[name] || (this._events[name] = []);
    events.push({callback: callback, context: context, ctx: context || this});
    return this;
  },

What does the line this._events || (this._events = {}); mean? For me, _events looks like an inner variable, but is (this._events = {}) used for assignment or is it an or comparison? Or is || a completely different operation in this context?

like image 495
Cherry Avatar asked Dec 20 '25 23:12

Cherry


2 Answers

It is a trick that uses javascripts "falsy" evaluation. It is the same as:

if (this._events) {
    // do nothing, this._events is already defined
} else {
    this._events = {};
}

The same goes for the line var events = this._events[name] || (this._events[name] = []); which could be translated to

var events;
if (this._events[name]) {
    events = this._events[name];
} else {
    this._events[name] = [];
    events = this._events[name];
}
like image 190
Christoffer Avatar answered Dec 23 '25 15:12

Christoffer


What line “this._events || (this._events = {});” means?

The logical OR (||) executes the first expression this._events and if falsy executes the second expression (this._events = {}).

In essence it checks if this._events is falsy and if so then assigns a new empty object to it.

That way no matter what this._events will always be at least an empty object and the code following will be able to execute without issues.

like image 27
Nope Avatar answered Dec 23 '25 16:12

Nope