Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fun with Ember, CoffeeScript precedence

IF

 !Ember.isEmpty @get('a') #-> true

AND

 !Ember.isEmpty @get('b') #-> false

Then why

 !Ember.isEmpty @get('a') and !Ember.isEmpty @get('b') #-> true

Or is it my CoffeeScript?

like image 368
yuяi Avatar asked Feb 10 '14 22:02

yuяi


2 Answers

The issue you're having is with Coffeescript precedence. Your code in javascript is:

!Ember.isEmpty(this.get('a') && !Ember.isEmpty(this.get('b')));

You need to add parens to make the order of operations more explicit here:

!Ember.isEmpty(@get('a')) and !Ember.isEmpty(@get('b'))

So in javascript it is:

!Ember.isEmpty(this.get('a')) && !Ember.isEmpty(this.get('b'));
like image 135
Tindron Avatar answered Nov 15 '22 09:11

Tindron


Everything after .isEmpty is interpreted as arguments for that function call. If that's not what you want, you need brackets or groups.

Also, you should use not instead of !, because of clarity.

If you only use brackets to group calls and their arguments, it becomes clearer:

not Ember.isEmpty(@get 'a') and not Ember.isEmpty @get 'b'

Or even clearer, although a bit lispy

(not Ember.isEmpty @get 'a') and not Ember.isEmpty @get 'b'
like image 45
Markus Avatar answered Nov 15 '22 09:11

Markus