Coffeescript || = analog?

I am primarily a Rails developer, and therefore, by hacking a little script for an instance of my Hubot company, I was hoping to do the following:

robot.brain.data.contacts ||= {} 

Or, just make this new hash if it does not exist yet. The idea is that I want to have an array of contacts added dynamically through a script, so I don’t need to change the source of the Hubot, and I obviously don’t want to overwrite any contacts that I add to it.

Question: is there a small construct like Rails || = which can I use in Coffeescript to accomplish this?

Greetings.

+43
ruby-on-rails coffeescript
Mar 05 '12 at 19:18
source share
5 answers

You can use ?= For conditional assignment:

 speed ?= 75 

? is the Existential Operator in CoffeeScript, so it will check for the existence (not plausibility):

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

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

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

Additional information: http://jashkenas.github.com/coffee-script/

+61
Mar 05 '12 at 19:21
source share
β€” -

I personally use or= instead of ?= Mainly because this is what I call ||= (or equal) when I use it in Ruby.

 robot.brain.data.contacts or= {} 

The difference is that or= short circuit when robot.brain.data.contacts not null , while ?= Checks for null and only sets robot.brain.data.contacts to {} if not null .

See compiled difference.

As mentioned in another post, none of the methods check for the existence of robot , robot.brain or robot.brain.data , but also the Ruby equivalent.

Edit:

Also in CoffeeScript or= and ||= compile the same JS.

+12
Mar 05 2018-12-12T00:
source share

?= will assign a variable if it is null or undefined .

Use it as speed ?= 25

+7
Mar 05 '12 at 19:21
source share

It is called the existential operator in Coffeescript and ?= , Http://coffeescript.org/ . Quoting below:

Existential operator

It's a little tricky to check for a variable in JavaScript. if (variable) approaches but fails for zero, empty string and false. Existential CoffeeScript operator? returns true if the variable is not null or undefined, which makes it similar to Ruby no?

It can also be used for safer conditional assignments than || = provides, in cases where you can process numbers or strings.

+4
Mar 05 '12 at 19:23
source share

The Coco CoffeeScript dialog, http://github.com/satyr/coco , supports the auto-suspension of the array and object @ and @@ :

 robot@brain@data@contacts.foo = 1 

compiles - provided, hairy -

 var _ref, _ref2; ((_ref = (_ref2 = robot.brain || (robot.brain = {})).data || (_ref2.data = {})).contacts || (_ref.contacts = {})).foo = 1; 

which ensures that every step of the path robot.brain , brain.data , data.contacts really exists.

Of course, you might just need a conditional assignment operator (which, according to the answers above, also exists in CoffeeScript):

 robot.brain.data.contacts ?= {} 

which compiles to

 var _ref; (_ref = robot.brain.data).contacts == null && (_ref.contacts = {}); 
+1
Mar 05 2018-12-12T00:
source share



All Articles