Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

javascript - ES6 arrow function lexical this in V8

I have the following ES6 code using a fat arrow function:

var test = {
  firstname: 'David',
  fn: function() {
    return ['one', 'two', 'tree'].map(() => this.firstname)
  }
}
console.log(test.fn())

According to how arrow functions are supposed to work I'd expect this to be the test object. ES6Fiddle, Traceur and Firefox produce the expected output which is ["David", "David", "David"].

When enabling those features in Chrome using chrome://flags/#enable-javascript-harmony, however, I get [undefined, undefined, undefined]. If you console.log(this) it shows that it is the window object and you get an error in strict mode. Is the lexical this for ES6 arrow functions not implemented in V8 yet?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Lexical this is the last part of ES6 arrow functions to land in v8 and it is the reason why it is still behind a flag and not ready to ship yet. Adrian Perez at Igalia is implementing arrow functions and the final patch is almost ready to land as soon as a few TurboFan issues are worked out: https://codereview.chromium.org/883823002/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...