JSFiddle - React, Tailwind, and code Playground
by bogdan_vq
JavaScript
The ideea is that CoffeeScript parses the 'for' statements something like this : ` for (_i = 0, _len = _ref.length; _i < _len; _i++) { //code } `.
Let's say I want to iterate throught JavaScript object properties using CoffeeScript but because the CS parser replaces the `for..in` loop with an simple `for` loop, the code will not be working:
#CoffeeScript
Object.prototype.keys = (o) ->
keys = (prop for prop in o)
keys
//JavaScript
Object.prototype.keys = function(o) {
var keys, prop;
keys = (function() {
var _i, _len, _results;
_results = [];
console.log('object length: ' + o.length); // undefined
for (_i = 0, _len = o.length; _i < _len; _i++) {
prop = o[_i];
_results.push(prop);
}
return _results;
})();
return keys;
};
Sure that the following will work fine in a separate JS file :
Object.prototype.getKeys_2 = function(o) {
var keys = [], prop = void 0;
for (var prop in o) {
if (o.hasOwnProperty(prop)) {
keys.push(prop);
}
}
return keys;
};
but for the further usses of `for..in` loop i wouldn't like to have the code in separate JavaScript files.
[jsFiddle][1]
[1]: http://jsfiddle.net/bogdan_vq/N3tQq/