by Excavator · 12y ago · 0 comments
for (key in object)
is for getting keys, in Firefox, meanwhile 'of' for (value of iterator)
is for getting values out of an Iterator.How do you mean when you state that for(of) is incorrect?
Perhaps this 'example' clears something up?
« for (value in {a:1,b:2,c:3,d:4,e:5}) console.log(value)
» undefined
"a"
"b"
"c"
"d"
"e"
« for (value of {a:1,b:2,c:3,d:4,e:5}) console.log(value)
× TypeError: ({a:1, b:2, c:3, d:4, e:5})['@@iterator'] is not a functionThese are legal CoffeeScript examples:
for key, value of object
if key of object
for value in array
if value in array
for index, value of array
if index of array
The one that's missing is: for value in object
if value in object every element as key, value from arr {
console.log(key, value);
}
every property as key, value from obj {
console.log(key, value);
} every [key, value] from arr {
console.log(key, value);
}
every {key, value} from obj {
console.log(key, value);
}
every [key] from arr {
console.log(key);
}
every {_, value} from obj {
console.log(value);
}
Looks a bit cramped to me but I get the impression that array comprehension is one of the major reasons that people use Coffeescript, so perhaps it's not too much trouble?There's also the issue with some fonts making it hard to differentiate between { and [ but it's an idea that might be worth to think about at least.
But if Coffescript can walk away with its choice then I don't think you'll have anything to worry about.