Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoffeeScript, What is the difference between => and -> [duplicate]

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.

like image 336
Tyler Avatar asked Sep 30 '13 16:09

Tyler


1 Answers

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;
};
like image 174
Daniel A. White Avatar answered Sep 17 '22 17:09

Daniel A. White