Re:How to Write CoffeeScript Efficiently
by nilbus on Tuesday November 29, 2011 @08:25AM
(#3290177)
Could someone explain how {@foo} = bar compiles into this.foo = bar.foo;?
[ Parent
| on HN ]
Re:How to Write CoffeeScript Efficiently
by yesbabyyes on Tuesday November 29, 2011 @08:41AM
(#3290230)
"@" is an alias for "this.", so "@foo" is the same as "this.foo".
{foo} = bar is using destructuring assignment, it means, take property "foo" of bar and put it in variable foo, so
{foo} = bar
compiles to var foo = bar.foo;Thanks! It was the destructuring part that I wasn't understanding right away.
Re:How to Write CoffeeScript Efficiently
by yesbabyyes on Tuesday November 29, 2011 @10:18AM
(#3290607)
No problem! CoffeeScript's destructuring is really nice, IMO:
[first, rest..., last] = someArray
professions =
programmer:
studyTime: "5 years"
salary: 10000
salesman:
studyTime: "2 years"
salary: 15000
{programmer: {salary}} = professions
console.log "A programmer's salary is %s", salary
EDIT: Fixed bug, "developer" should be "programmer".