Poll: Have you moved from JavaScript to CoffeeScript?
141–150 of 178 posts
Re: Poll: Have you moved from JavaScript to CoffeeScript?
#142Earlier quoted context omitted.
> Do you have an example of a real-world piece of code that wants to access both the lexical and dynamic "this" Yes. > and isn't a jQuery callback? No :^). And I wish JQuery ran the callback with a parameter too, so I could access 'this' to refer to the class and use the argument to refer to the object I'm going to run the callback on. You're totally right that would be neater. Alas Coffeescript is a lot newer than J…
You're in luck then. This is precisely what the fat arrow in CoffeeScript is for: creating a function where the value of "this" is bound lexically. So: class Widget render: -> $(".widget-title").click (e) => # `this` is still the Widget instance. # `e.currentTarget` is the DOM element.
click() works because the callback is given parameter:
.click( handler(eventObject) )
Cool. You access @ for the class, and eventObject for the thing you clicked.But I wasn't using click(), I was using load(), which has a different syntax:
.load( url, [data,] [complete(responseText, textStatus, XMLHttpRequest)] )
I may be missing something - I only started doing JS seriously last year - but 'complete' doesn't seem to be provided with any parameters, like the element I just loaded. So my only choice, AFAICT, is to use 'this' to access that element. I'd be delighted if that was wrong though. :^)Background: I was trying to get the width of an image file that wasn't in the document, so I could use it to size another image (which happened to use the first image as a webkit mask). Great fun.
Re: Poll: Have you moved from JavaScript to CoffeeScript?
#143Being a Ruby shop, CoffeeScript (CS) has been a choice for us. We simply find it is a good fit for our minds and how we like to write code, makes it that much easier to structure and organize code nicely, and is more pleasant to look at day in and day out.
Re: Poll: Have you moved from JavaScript to CoffeeScript?
#144Earlier quoted context omitted.
Yah but your example for Javascript uses some dubious code. Did you know that "for .. in" loops are also native to Javascript? Why not use one for JS, why only Coffeescript? Also, it seems like you are doing some useless variable declrations. Let me rewrite it for you: 1) var list = [{amount:2}, {amount:5}], i, sum = 0; for (i = 0; i console.log(sum); 2) Or let's go even more simple... var list = [{amount:2}, {amount…
JavaScript's for-in construct is not the same as iterating through all elements in the array as it will enumerate over all of the object's properties (see here: http://stackoverflow.com/questions/500504/javascript-for-in-... ) The other "complexities" in the compiled JavaScript are there for perf reasons (not accessing the "length" properties in the condition portion of the for loop, for example). Your comment underl…
And I agree that accessing the length property in the conditional is not ideal.
I guess it comes down to this for me: I don't feel that Javascript has an unduly burdensome list of common idiosyncrasies to justify developing in a 'different language'/syntax that compiles into javascript and changing my development workflow to reap said questionably useful benefits.
Perhaps I am simply not the intended audience.
Re: Poll: Have you moved from JavaScript to CoffeeScript?
#145Earlier quoted context omitted.
Titanium is a mixed bag. For simple applications, it's great, but as soon as you need to do something a little bit complicated, the learning curve goes up quite a bit. For example, in my app ( http://iphone.albumpl.us ) I had to write a couple of small custom objective-c modules to get some camera/image stuff to have sufficient performance. But overall being able to write most of it in CoffeeScript, is so much better…
Ah, very nice, thank you.
Re: Poll: Have you moved from JavaScript to CoffeeScript?
#146I'm the author of the PragProg book on CoffeeScript: http://pragprog.com/titles/tbcoffee/coffeescript It's heartening to see so many people responding to this poll. I hopped on the CoffeeScript bandwagon more than a year ago; a week of using it was enough to convince me to stop writing pure JS. It's become my favorite language, even overtaking Ruby. (Cue dhh at last month's RailsConf keynote: "Looking at CoffeeScript…
Re: Poll: Have you moved from JavaScript to CoffeeScript?
#147One of the interesting things on doing some analytics on our customer base was seeing just how wide the gap is between the usual HN fare and reality: http://blog.directededge.com/2010/05/30/what-programming-lan... The core message being: things like Scala, Erlang, et al still represent a tiny portion of the actual tech world. (And our customers tend to be biased towards being techy anyway.) Just because CoffeeScript…
That shouldn't be surprising. A willingness to learn new and esoteric languages is one of the hallmarks of HN-ers. See also: The Python Paradox ( http://www.paulgraham.com/pypar.html ).
Re: Poll: Have you moved from JavaScript to CoffeeScript?
#148Earlier quoted context omitted.
Is it difficult to debug the resulting code?
Browser implementors are working on making easier to debug all compile-to-JS languages, including regular minified JavaScript code. https://bugzilla.mozilla.org/show_bug.cgi?id=618650 https://bugs.webkit.org/show_bug.cgi?id=30933 In the meantime, CoffeeScript tries to make debugging easy by compiling to straightforward, readable JS, with meaningful variable names, normal indentation, and all that. Patches to make the…
http://jashkenas.github.com/coffee-script/#loops
To be clear, I think CoffeeScript is an impressive language/abstraction. I just get tired of people selling the Javascript output as always being "straightforward" and "readable".
Re: Poll: Have you moved from JavaScript to CoffeeScript?
#149One of the interesting things on doing some analytics on our customer base was seeing just how wide the gap is between the usual HN fare and reality: http://blog.directededge.com/2010/05/30/what-programming-lan... The core message being: things like Scala, Erlang, et al still represent a tiny portion of the actual tech world. (And our customers tend to be biased towards being techy anyway.) Just because CoffeeScript…
Re: Poll: Have you moved from JavaScript to CoffeeScript?
#150Earlier quoted context omitted.
Browser implementors are working on making easier to debug all compile-to-JS languages, including regular minified JavaScript code. https://bugzilla.mozilla.org/show_bug.cgi?id=618650 https://bugs.webkit.org/show_bug.cgi?id=30933 In the meantime, CoffeeScript tries to make debugging easy by compiling to straightforward, readable JS, with meaningful variable names, normal indentation, and all that. Patches to make the…
Strait forward and readable in many cases but beware: http://jashkenas.github.com/coffee-script/#loops To be clear, I think CoffeeScript is an impressive language/abstraction. I just get tired of people selling the Javascript output as always being "straightforward" and "readable".
The only difference from a "hand-written" loop is that you would use `i` and `ln` instead of `_i` and `_len`, and something in context like `numbers` instead of `_results`.