6

I am new to CoffeeScript. I have run into this today.

example -> 
 a ->

and

example ->
 b =>

What this the different between a thin arrow vs a fat arrow?

Could someone please explain the difference and when they should be used.

Tyler
  • 678
  • 7
  • 22

1 Answers1

12

The fat arrow => defines a function bound to the current value of this.

This is handy especially for callbacks.

Notice the generated differences

Coffee script:

foo = () -> this.x + this.x;
bar = () => this.x + this.x;

JavaScript

var bar, foo,
  _this = this;

foo = function() {
  return this.x + this.x;
};

bar = function() {
  return _this.x + _this.x;
};
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445