从扩展的 Electron 类调用类方法时出错

问题描述

首先,我是 JavaScript 和 Electron 的新手。 这是一个代码片段,用于复制 YouTube 上基本的 Javascrip/Electron 教程中的概念,但由于某种原因对我不起作用。

一切都按预期进行。现在,通过扩展 Tray(Electron 类)定义一个派生类并执行相同的操作:

我在脚本中遗漏了什么吗?为什么会发生这种情况?

注意 1:我对调用扩展类方法的替代编码解决方案不感兴趣,除非有明确的迹象表明,根据设计,派生 Electron 类破坏了这里使用的编码解决方案片段。

您可以在此处找到教程:How To Code - Electron JS Tutorial #46

节点版本:15.8.0 - 电子版本:11.2.3

重现问题的代码

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)
}) 

解决方法

除非有明确的迹象表明,根据设计,派生 Electron 类会破坏此代码段中使用的编码解决方案。

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

我们不支持在 Electron 中扩展内置类

Electron 中内置的类不是普通的 javscript 对象,不支持扩展类等所有 js 行为。