Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES6 transpilation of arrow functions #2853

Closed
digitalnature opened this issue Mar 16, 2018 · 1 comment
Closed

ES6 transpilation of arrow functions #2853

digitalnature opened this issue Mar 16, 2018 · 1 comment

Comments

@digitalnature
Copy link

I was testing the es6 transpilation feature on https://closure-compiler.appspot.com
and I noticed that the arrow functions are not bound to "this" in the output code. Is this normal?

Eg.

let x = 2;
f(v => {
  return x + v;
});

Output is:

var x = 2;
f(function(a) {
  return x + a;
});

But I think it should be:

var x = 2;
f((function(a) {
  return x + a;
}).bind(this));
@MatrixFrog
Copy link
Contributor

If the function never references this then it doesn't matter. If it does, then .bind(this) would be one way of transpiling it, but we went with a different way:

https://closure-compiler-debugger.appspot.com/#input0%3Df(()%2520%253D%253E%2520alert(this))%253B%26input1%26conformanceConfig%26externs%3Dvar%2520alert%252C%2520f%253B%26refasterjs-template%26includeDefaultExterns%3Dtrue%26CHECK_SYMBOLS%3Dtrue%26MISSING_PROPERTIES%3Dtrue%26TRANSPILE%3Dtrue%26CHECK_TYPES%3Dtrue%26CLOSURE_PASS%3Dtrue%26PRESERVE_TYPE_ANNOTATIONS%3Dtrue%26PRETTY_PRINT%3Dtrue

As I recall, we heard from someone on the V8 team that this pattern (the "var self = this" pattern) is probably faster in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants