webpack: [4] Webpack turns "this" to undefined in class definitions
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
See https://github.com/webpack/webpack/issues/6357#issuecomment-360308225 and https://github.com/babel/babel/issues/7264
Webpack turns the method in this code:
import React from 'react'
class A {
constructor() {
this.b = 1
}
method(a = this.b) {
console.log(a)
}
}
into
method(a = undefined.b) {
console.log(a)
}
probably confusing the top-level with class-level. Curiously without the import (any import) it seems to be fine.
Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.
MacOS High Sierra, Node 9.4.0, Webpack 4 beta.0
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 17 (7 by maintainers)
@ooflorent Has this fixed been released in current webpack version. My webpack version is 4.29.0 and the same thing happening to me. I have imported one of my project’s dependency i.e. jquery plugin and within that file in class definition they passed “this” alias for window but webpack turned that into “undefined”.
I’ve opened a PR with a fix. Everything should be back to normal once it would be merged.
After a bit of reading I think this is Babel to blame - https://stackoverflow.com/questions/34973442/how-to-stop-babel-from-transpiling-this-to-undefined-and-inserting-use-str