question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[4] Webpack turns "this" to undefined in class definitions

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
kamalendugaraicommented, Jan 30, 2019

@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”.

2reactions
ooflorentcommented, Apr 5, 2018

I’ve opened a PR with a fix. Everything should be back to normal once it would be merged.

Read more comments on GitHub >

github_iconTop Results From Across the Web

'this' is undefined in JavaScript class methods - Stack Overflow
I just wanted to point out that sometimes this error happens because a function has been used as a high order function (passed...
Read more >
Optimization - webpack
webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable...
Read more >
TS Playground - An online editor for exploring TypeScript and ...
Check for class properties that are declared but not set in the constructor. ... Enable error reporting when this is given the type...
Read more >
webpack class extends value undefined is not a constructor or ...
It is possible you are creating some sort of circular dependency loop by the way you import external modules in index.js. But, it...
Read more >
How to Read React Errors (fix 'Cannot read property of ...
This error usually means you're trying to use .map on an array, but that array isn't defined yet. That's often because the array...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found