How to use Javascript's for (attr in this) with Coffeescript?

Instead of for item in items which iterates through arrays, you can use for attr, value of object which works more like for in from JS.

Instead of for item in items which iterates through arrays, you can use for attr, value of object, which works more like for in from JS. For own attr, value of this if attr == 'foo' && value == 'bar' console. Log 'Found a foobar!' Compiled: https://gist.github.

Com/62860f0c07d60320151c It accepts both the key and the value in the loop, which is very handy. And you can insert the own keyword right after the for in order to enforce an if object. HasOwnProperty(attr) check which should filter out anything from the prototype that you don't want in there.

Exactly right. CoffeeScript's for...of compiles directly to JavaScript's for...in. CoffeeScript's for...in is for iterating over the values in arrays, strings, and other zero-indexed structures with a length property.

– Trevor Burnham Apr 22 at 17:37.

Squeegy's answer is correct. Let me just amend it by adding that the usual solution to JavaScript's for...in being "dangerous" (by including prototype properties) is to add a hasOwnProperty check. CoffeeScript can do this automatically using the special own keyword: for own attr of this ... is equivalent to the JavaScript for (attr in this) { if (!Object.prototype.

HasOwnProperty(this, attr)) continue; ... } When in doubt about whether you should use for...of or for own...of, it's generally safer to use own.

You can use for x in y or for x of y depending on how you want to interpret a list of elements. The newest version of CoffeeScript aims to solve this problem, and you can read about its new use with an issue (that has since been implemented and closed) here on GitHub.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions