4

I'm working on a project using Easel JS. Opened up the Easel file and the very first line of code confused me:

this.createjs = this.createjs||{};

I know that createjs is invoked when you're setting up your canvas or, for example, creating a bitmap to add to the canvas. But I don't understand the syntax of this line - assign this.createjs or (what I guess is) a blank object to this.createjs?

unaesthetic
  • 108
  • 1
  • 8

4 Answers4

6
this.createjs = this.createjs||{};

If this.createjs is not available/ any falsy value then you are assigning {} empty object to this.createjs.

It's more like,

var a, 
    b;

b = a || 5;

Since a is not having any value currently, 5 will be assigned to b.

mohamedrias
  • 18,326
  • 2
  • 38
  • 47
2

Correct. This ensures that if this.createjs doesn't already exist, an empty object is assigned to it. The || is an or operator - if this.createjs on the left-hand-side evaluates to falsy, it will assign the right-hand-side instead.

Andrew Ferrier
  • 16,664
  • 13
  • 47
  • 76
2
this.createjs = this.createjs||{};

If this.createjs is falsy, this.createjs will be a new empty object

You could have replace it by

if (!this.createjs){
     this.createjs = {};
}
Deblaton Jean-Philippe
  • 11,188
  • 3
  • 49
  • 66
1

|| means or. In that context means this.createjs equals if exists/not null/defined this.createjs other way {}

naoxink
  • 595
  • 1
  • 12
  • 19