Operators "in" and "of" are very inconsistent between CoffeeScript and JavaScript

Posted . Visible to the public.

CoffeeScript and JavaScript (ECMAScript) both have operators in and of. Each language use them for more than one purpose. There is not a single case where the same operator can be used for the same purpose in both languages.

Check if an object (or its prototype) has a property

CoffeeScript

var hasFoo = 'foo' of object

JavaScript

var hasFoo = 'foo' in object;

Iterate through all properties of an object

CoffeeScript

object = { ... }

for key, value of object
  console.log("key %o has value %o", key, value);

JavaScript

var object = { ... };

for (var key in object) {
  var value = x[key];
  console.log("key %o has value %o", key, value); 
}

Iterate through all values of an array

CoffeeScript

array = [...]

for entry in array
  console.log("value is %o", entry.value)

JavaScript

var array = [...]

for (var entry of array) {
   console.log("value is %o", value); 
})

Iterate through all values of an iterator

CoffeeScript

iterator = ...

while entry = iterator.next() && !entry.done
  console.log("value is %o", entry.value)

JavaScript

var iterator = ...;

for (var value of iterator) {
   console.log("value is %o", value); 
}
Henning Koch
Last edit
Florian Leinsinger
Keywords
different, differently
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2018-05-02 08:38)