Coffeescript ||= analogue?

You can use ?= for conditional assignment:

speed ?= 75

The ? is the “Existential Operator” in CoffeeScript, so it will test for existence (not truthiness):

if (typeof speed === "undefined" || speed === null) speed = 75;

The resulting JS is a bit different in your case, though, because you are testing an object property, not just a variable, so robot.brain.data.contacts ?= {} results in the following:

var _base, _ref;
if ((_ref = (_base = robot.brain.data).contacts) != null) {
  _ref;
} else {
  _base.contacts = {};
};

More info: http://jashkenas.github.com/coffee-script/

Leave a Comment