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
'this' becomes 'undefined' in es2015 preset #7636
Comments
Hey @thany! We really appreciate you taking the time to report an issue. The collaborators If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack |
In ES modules global |
I'm not so sure about that actually. The global |
The global |
I don't think that's a good way to think about it. The module-level execution environment, which is a level above the global scopes, specifically sets https://www.ecma-international.org/ecma-262/8.0/#sec-module-environment-records-getthisbinding |
@nicolo-ribaudo But that still doesn't answer the question: why replace Why does anyone need @loganfsmyth |
This behavior happens in
Right now Babel simply assumes that the file is an ES module. I 100% agree that this is not ideal, and we're changing that in Babel 7. |
How would it be incorrect to leave it as-is? How could that possibly be? If |
Sorry that |
Ah, I see. Well then it should definitely not be replaced. Just leave it as-is so a script can use it as the |
Like I said, we're working on this in Babel 7. If a file isn't marked as an ES module, this transform won't happen. If you want to use |
I get that, but I'm saying this transform doesn't ever need to happen. |
@thany It need to happen with |
Yeah, that is not the case. ES modules explicitly see |
Whaterver ES modules see |
Bug: when using
this
at the global level of your js code, using thees2015
orenv
preset, it will be transformed toundefined
, which is not correct.Input Code
Output
Babel Configuration
Calling babel via gulp:
No config file otherwise.
Expected Behavior
this
should not be transformed intoundefined
. There is no reason to.Current Behavior
this
at the global context is transformed intoundefined
.Possible Solution
It works fine in the
es2017
preset, so perhaps copy over what that preset is doing, into es2015.Context
I'm trying to transform modern javascript into javascript that oldIE can understand, and the above code is how we write our top-level IIFE to avoid conflicts in the global context.
this
in the global context means thewindow
object.Your Environment
Noet: the issue can also be reproduced on the Try it out thing.
The text was updated successfully, but these errors were encountered: