Skip to content

Instantly share code, notes, and snippets.

@medikoo
Created April 13, 2012 08:42
Show Gist options
  • Save medikoo/2375144 to your computer and use it in GitHub Desktop.
Save medikoo/2375144 to your computer and use it in GitHub Desktop.
JavaScript fat arrow function
// ES6:
var Foo = {
getValue: () => "Foo value",
somethingUseful: () => this.getValue();
}
// same in ES5:
var Foo = {
getValue: function () { return "Foo value"; }
};
Foo.somethingUseful = function () { return this.getValue(); }.bind(Foo);
// it may not work as expected with inheritance:
var Bar = Object.create(Foo, {
getValue: { value: function () { return "Bar value" } }
});
Bar.somethingUseful() // => "Foo value"
@medikoo
Copy link
Author

medikoo commented Apr 13, 2012

Yeah, it may seem more logical, but I think they wanted to address also other use case, and solve two of them at once:

  1. writing function () { .. } for simple one line functions is too verbose (but it's not that issue for multistatement functions)
  2. If we want to relate to context in such simple function in most cases it is lexical parent context (writing extra .bind(this) adds to already too verbose statement)

So I see it as remedy for that, additionally we'll get some sugar for descriptors and method declarations, it looks ok for me :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment