Search code examples
javascriptelectronextendsderived-classmethod-call

Error calling class method from extended Electron class


First of all, I'm a newbie on javascript and Electron. This is a code snippet to replicate a concept coming from a basic Javascrip/Electron tutorial on YouTube, that for some reason is not working for me.

  • Define a base class "base"
  • Define a class called "derived" that extends "base"
  • In derived constructor, call super() and then call the derived class method this.showSomething()

Everything works as expected. Now, define a derived class by extending Tray (Electron class) and do the same things:

  • Define a class called "derivedFromTray" that extends "Tray"
  • In derived constructor, call super() and then call the derived class method this.showSomething() This leads to an error: this.ShowSomething is not a function

Am I missing something in the script? Why this happens?

NOTE 1: I'm not interested in alternative coding solutions to call the extended class methods, unless there is a clear indication that, by design, deriving Electron classes breaks the coding solution used in this snippet.

You can find the tutorial here: How To Code - Electron JS Tutorial #46

Node version: 15.8.0 - Electron version: 11.2.3

Code to replicate the issue:

const electron = require('electron')
const path = require('path');
const {app, Tray} = electron;

class base{
    constructor(path){
        this.internalPath = path        
    }
    
}

class derived extends base {
    constructor(path) {
        super(path)
        this.showSomething();
    }
    showSomething() 
    {    
        console.log(`internal Path: ${this.internalPath}`);
    }
}

class derivedFromTray extends Tray {
    constructor(iconPath) {
        super(iconPath)
        this.showSomething() // ERROR: this.showSomething is not a function
    }
    showSomething() 
    {    
        console.log('Log from derivedFromTray.showSomething');
    }    
}

app.on('ready', ()=>{
    // WARNING: specify a valid image path or the example
    // will not work!
    const paramPath = path.join(__dirname, "app_tray_icon.png")
    const workingDerived = new derived(paramPath)
    const notWorkingDerived = new derivedFromTray(paramPath)
}) 


Solution

  • unless there is a clear indication that, by design, deriving Electron classes breaks the coding solution used in this snippet.

    https://github.com/electron/electron/issues/25721

    we do not support extending built in classes in Electron

    Built in classes in Electron are not plain javscript object and do not support all js behaviors like extending class.