To put it mildly, JavaScript has some idiosyncrasies. I've enjoyed manipulating, and exploiting some of JavaScript's more interesting aspects. However, it's uniqueness can cause confusion. It is perfectly fine to those with experience with JavaScript, but they often trip up even experience programmers that aren't well versed in the language.
Concerns about debugging
One major advantage of JavaScript development is how well debugging is integrated. Chrome and now Firefox debugging is pretty awesome. The only comparable interfaces I've used are Eclipse and XCode, and Chrome preforms better than either of these.Initially I was concerned that the change in syntax and line numbers would make debugging difficult. Put simply, this turns out to this is much less of an issue than I anticipated. It is trivial to identify corresponding CoffeeScript code working backward from compiled JavaScript.
Class creation
It starts with simple class creation. In JavaScript there isn't anything to distinguish a Class from any other named function.var MyClass = function(){};
That is, until you add methods to it.MyClass.prototype.foo = function(){};
This is just weird.CoffeeScript makes it a little more clear what is a class.
class MyClass
foo: ->
Which this is this
Here's a common scenario. I want to bind an instance method of an object to listen to an event (Mouse Click, AJAX response whatever). In java script I have to store this in a local variable so that I can use it later. Looks something like this.var MyClass = function(){
var t = this;
$("a.bindo").click(function(){
t.onClick();
})
}
MyClass.prototype.onClick = function(){
console.log("This is me", this);
}
I've used that pattern hundreds of time. It is useful, but again confusing to someone new to the language. The CoffeeScript version, by contrast, is succinct.class MyClass
constructor: ->
$("a.bindo").click @onClick
onClick: =>
console.log("This is me", @)
It is true that the developer has to understand on a conceptual level what is happening with the bind event, but I find that a simple character difference is more readily accessible and apparent than the JavaScript pattern.Inheritance
CoffeeScript inheritance murders JavaScript inheritance. I have an older post on JavaScript prototype inheritance. It works (kinda), but again is unfamiliar to people coming from other languages.CoffeeScript inheritance is easy, and offers something I've not seen cleanly executed in raw JavaScript. The super keyword in CoffeeScript called the super
in JavaScript to call the parent method it looks something like this.
var MyClass = function(){
ParentClassName.call(this, arguments)
}
Now I think call and apply are super dope, but most of the time I would prefer to never touch them. Now the CoffeeScript equivalentclass MyClass extends ParentClassName
constructor: ->
super
That's is way easier, and way less likely to cause hard to track down bugs.
No comments:
Post a Comment