1

I've got a little problem occurring with super and functions overriding the parent ones.

A simplified example of my problem is the following, having 2 files:

FileA.js

constructor () {
    ...
}
myFunction = () => {
    ...
}

FileB.js

class FileB extends FileA {
    constructor () {
        ...
    }
    myFunction = () => {
        if () {
            // do something
        } else {
            super.myFunction()
        }
    }
    render () {
        return <button onClick={() => this.myFunction()}
    }
}

First of all, if the else condition is invoked from FileB.js, an error is launched:

TypeError: (intermediate value).myFunction is not a function

But if I make the myFunction of FileB.js a non-arrow function, this one is never fired, and instead is fired the one from FileA.js.

Why is this happening and how to solve it?

Zander
  • 1,076
  • 1
  • 9
  • 23
  • How you are calling `myFunction` in `FileB`? – ravibagul91 Aug 20 '19 at 08:23
  • Can you show more code, what do you have in "myFunction" and what do you have in FileB? – Iiskaandar Aug 20 '19 at 08:33
  • Possible duplicate of [Using \`super\` within an arrow function within an arrow function within a method](https://stackoverflow.com/questions/32943776/using-super-within-an-arrow-function-within-an-arrow-function-within-a-method) – Carlos Sá Aug 20 '19 at 08:43
  • @ravibagul91 I'm calling it as simple as the FileB.js shows (updated) – Zander Aug 20 '19 at 08:58
  • @Iiskaandar there is not much to show. It's not the code inside the if statement from FileB.js, it works correctly. The only thing that occurs wrong is the `super.myFunction()`. – Zander Aug 20 '19 at 09:05
  • @cheshire if there is not much to show, can you print it to some js playground or something? Then we could see what exactly is working wrong. – Iiskaandar Aug 20 '19 at 09:07
  • 1
    __super is lexically scoped, just like this to the closest enclosing function that defines it. All function definition forms except for arrow functions introduce new this/super bindings so we can just [say] that this/super binds according to the closest enclosing non-arrow function definition__ Simply put, if you use an arrow function it has no context and `super` won't work. – Carlos Sá Aug 20 '19 at 10:49

0 Answers0