Such a question needs a little explanation ...
ECMAScript 5
There were no arrow functions in the ES5 specification . Then it was customary to use traditional function expressions, for example:
var myFunction = function () {
return 'Hello!';
};
var obj = {
myFunction: function () {
return 'Hello!';
}
};
var arr = ['foo', 'bar'];
arr.map(function (item) {
return 'Hello, ' + item + '!';
};
CoffeeScript
CoffeeScript , , (->) (=>).
, CoffeeScript ES5 (). CoffeeScript :
myFunction = -> 'Hello!'
obj =
myFunction: -> 'Hello!'
arr = ['foo', 'bar']
arr.map((item) -> "Hello, #{item}!")
, - , CoffeeScript, ES5. , , . CoffeeScript jQuery. , :
// Here "this" is "window"
console.log(this);
$(document).ready(function () {
// Here "this" is "document"
console.log(this);
});
"this" , ES5:
var that = this;
$(document).ready(function () {
console.log(that);
});
CoffeeScript :
// "this" is "window"!
$(document).ready => console.log this
, :
// "this" is "document"
$(document).ready -> console.log this
ECMAScript 6 (2015)
ES2015 . CoffeeScript. ES6 , , CoffeeScript. ES6 :
// Here "this" is "window"
$(document).ready(() => console.log(this));
, , ES5:
$(document).ready(function () {
console.log(this);
});