-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Description
Since the ES6 update has been merged already, I am moving a discussion out of #3874 and into an issue so that it is easier to track.
The issue as outlined by @limzykenneth in the pull request discussion:
Great work @hsab!
I have a particular question on the use of arrow functions to define prototype members. From what I can find it seems to affect the value of
thisin the member function (arrow function'sthisis defined when the function is evaluated, not when the object is created as such it doesn't point to the object instance created). For a discussion about it here and here.I can't identify with a quick look whether it is a problem here or not but if the use of arrow function on prototype member isn't really doing anything other than saving bytes it seems a bit superflous to me.
@hsab replied:
@limzykenneth Thanks!
This is certainly a valid and concerning point. I will have a thorough look tomorrow.
Although I was aware of this behavior, I can't recall running into any issues with this.
Also, just did a quick look and it seems like exactly what you mention is happening inmain.js. But these functions (_start,_draw,_setup) are critical (right?) and yetthisseems to point to object instance during execution. This is quite a curious case. Definitely needs more investigation. If you have an idea of why, I would very much appreciate it.
followed by @limzykenneth :
@hsab From my understanding, basically in the
main.jscase you pointed out, at the time the arrow function is evaluated, the value ofthisis the same as the value ofthis._start'sthissince they are defined in the same scope and so share the same value forthis. However, when attaching to the prototype, the value ofthisshould be defined at the point of object creation (whennew Something()is called) but with arrow functions, the value ofthisin the function will be defined where the function is defined.So in simpler terms, the issue will be centered around the usage of arrow functions on prototype members, the rest should not encounter problems and in my opinion ok to use arrow functions.
Note that this doesn't seem urgent so feel free to continue discussion and response at your own convenience. I just wanted a better place to track the topic. Thanks!