CoffeeScript Fat Arrow BUSTED

3686 days ago, 0 views.

CoffeeScript is much more than a simple language that compile to Javascript: It makes my code clean, readable and more easy to maintainable.

I try to give my code pure object-oriented approach. With CoffeeScript is more easy, but sometimes is difficult too.

When you’re handling callbacks you can not see clearly when to use -> or => and you need you study your code for understand better what’s happens.

So, what is => ?

=> as known as fat arrow is the language helper to bind in the reserved word this the external context to make accesible from the internal context.

In others (javascript) words:

var _this = this;

el.addEventListener("click", function(event) {
  return _this.handler(event);
});

and is very common in callbacks. But when you actually use?

Rule 1: You Don't Need the Fat Arrow If You Don't Use: class, this, and @.
Rule 2: Use the Fat Arrow when You Use @ in a Callback Definition in a Method.
Rule 3: Don't Use Methods as Callbacks and Avoid the Fat Arrow Operator in All Other Circumstances.

maybe the three rules can be resume in:

If callback and @, use arrow the fat.

or freak version:

If callback? and callback.use '@' then => else ->

This is a very simple example that I like to remember with the rules:

And ALWAYS remember that the key here is the context.

Context in CoffeeScript, and JavaScript, generally follows a few simple rules. Take a function named makeManhattan:

The context is the thing to the left of the dot. Here, bartender is the context.
eg: bartender.makeManhattan()

If there's nothing to the left of the dot, the context is the global context.
eg: makeManhattan()

If the function is an event handler or callback, the context is the event owner or caller.
eg: button.onClick(makeManhattan)
Source: 1 2
Kiko Beats

Kiko Beats