6 ms·
Edit: previous example was incorrect as pointed out by klibertp below. Hopefully they're correct now... it's not exactly a shining example of good coffeescript
by cubicle67 14y ago
Edit: previous example was incorrect as pointed out by klibertp below. Hopefully they're correct now...
it's not exactly a shining example of good coffeescript, but I think this is equivalent js
MyApp.president = Ember.Object.create({
fullName: function(){
return this.get('firstName') + ' ' + this.get('lastName');
}.property('firstName', 'lastName')
})
I'd probably write it in cs as
MyApp.president = Ember.Object.create
fullName: (
-> "#{@get 'firstName'} #{@get 'lastName'}"
).property('firstName', 'lastName')
(ie multi line rather than a single line) but that's very much just stylistic choice. It's also a lousy example of the benefits of cs as the cs version doesn't add any benefit over the plain js one.
- klibertp 14y agoYou're wrong. The 'property' is a method on the Function prototype - take a closer look. Anyway, style does matter and were this line written with readability in mind you wouldn't make this mistake. Also, I would definitely abstract over @get and property: makeProperty = (attrs...) -> func = () -> (@get(x) for x in attrs).join(" ") func.property(attrs...) MyApp.president = Ember.Object.create( fullName: makeProperty("firstName", "lastName") ) The parens after create are not needed, the next indented line should tell us that this is a function with arguments, but I don't mind them here.
- cubicle67 14y agogah! you're right! I've attempted to update my comment with corrected code, but given my caffeine and sleep deprived state there's a good chance it's just as wrong as my original effort :/
- knieveltech 14y agoIf it takes that much effort to gin up example code I think the point has been made.
- klibertp 14y agoYou're joking, right? If not, then you're trying to generalize one line of shitty code and one mistake of a tired programmer to the whole language - and I don't believe you'd want to do that!
- avenger123 14y agoClever or hard to read code is just that. I don't use CoffeeScript but I wouldn't use this example to be mean about it. At the end of the day, it is an acquired taste. My only concern is I don't have the time to invest to make a decision on it and I would suspect others are in the same boat. So, by default we choose a tentative no.