Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does question mark equals mean in CoffeeScript?

In this line of code of an Backbone app:

window.App ?= {}

what does ?= mean? Is is something like initialization of a Backbone app with empty defaults?

I'm new to Backbone/CoffeeScript and I read that an empty app (in CoffeeScript) would look like:

window.MyApp =
  Models: {}
  Collections: {}
  Views: {}
  Routers: {}
  initialize: -> 
    #do stuff

So is the first a shorthand for the second?

like image 624
Alexander Popov Avatar asked Apr 29 '14 14:04

Alexander Popov


1 Answers

It is the existential operator in CoffeeScript and would be (almost) equivalent to this JS

window.App = window.App || {};

It ensures that your App namespace is defined.

like image 59
nikoshr Avatar answered Oct 19 '22 02:10

nikoshr