This teaspoon of Coffee …
_pickInConf = (sourceConf,propsToPick...) ->
newConfWithPickedProperties = {}
newConfWithPickedProperties[key] = sourceConf[key] for key in Array::.concat.apply Array::,propsToPick when key in sourceConf
newConfWithPickedProperties
… gets compiled into :
_pickInConf = function() {
var key, newConfWithPickedProperties, propsToPick, sourceConf, _i, _len, _ref;
sourceConf = arguments[0], propsToPick = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
newConfWithPickedProperties = {};
_ref = Array.prototype.concat.apply(Array.prototype, propsToPick);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
key = _ref[_i];
if (__indexOf.call(sourceConf, key) >= 0) {
newConfWithPickedProperties[key] = sourceConf[key];
}
}
return newConfWithPickedProperties;
};
… and makes use of :
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
I expected Coffeescript compiler to transcompile this chunk :
when key in sourceConf
into :
if (key in sourceConf) {
… Which is what i meant to code in JS …
Is there a way i can force Coffescript to output this kind of JS ? Or to make it understand that sourceConf is an object, not an array ?
Thanks in advance for your answers !
You want
ofnotinto iterate keys of an object: http://coffeescript.org/#loops (near the end of the section)To expand on this:
compiles to
which is using the
a in objyou are looking for