Extending the static class : 未捕获的 TypeError:Super 表达式必须为 null 或函数

huangapple go评论69阅读模式
英文:

Extending the static class : Uncaught TypeError: Super expression must either be null or a function

问题

以下是您的源代码的中文翻译:

我对扩展类和 `static` 感到困惑

有两个 `static` `Main1`  `Main2`它们都是从 `Base` 类继承而来

每个类都有变量 `val``Main1.val`  `Main2.val` 应该是独立的不同值

以下是我的源代码

    class Base{
        static val;
        static initialize(x){
            this.val = x;
        }
    }
    class Main1 extends Base{
        static showVal(){
            return this.val;
        }
    }
    
    class Main2 extends Base{
        static showVal(){
            return this.val * 2
        }
    }
    
    
    var main1 = new Main1();
    var main2 = new Main2();
    
    main1.initialize(1);
    main2.initialize(2);
    
    console.log(main1.showVal());// 应该显示 1
    console.log(main2.showVal());// 应该显示 4

然而,这段代码显示以下错误。

如何解决这个问题?

Uncaught TypeError: Super expression must either be null or a function

如果您需要更多关于这个错误的帮助,请提供更多上下文,我将尽力协助您解决问题。

英文:

I am confused with extended class and static.

There are two static classes Main1 Main2, both are extended from Base class.

And each has the variable val. Manin1.val and Main2.val should be different value independently.

These are my source code below.

class Base{
    static val;
    static initialize(x){
        this.val = x;
    }
}
class Main1 extends Base{
    static showVal(){
        return this.val;
    }
}

class Main2 extends Base{
    static showVal(){
        return this.val * 2
    }
}


var main1 = new Main1();
var main2 = new Main2();

main1.initialize(1);
main2.initialize(2);

console.log(main1.showVal());// should show 1
console.log(main2.showVal());// should show 4

Howeber this code shows the error like this below.

How can I solve this?

    Uncaught TypeError: Super expression must either be null or a function

答案1

得分: 1

类是具有能力和属性的集合。在能力/属性方面,有两种主要类型:

  • 静态(类级别)
  • 实例级别

现在,static 方法是类的能力,而不是其实例的能力。由于您将 initialize 定义为 static 方法,它将无法通过类的实例访问,因为它不是实例级别的,而是类级别的能力(确切地说,是方法)。因此,我已经通过确保 main1main2 分别是 Main1Main2 的确切类,而不是实例,来修复了您的代码,然后可以访问它们的静态方法。如果您想使用实例并将这些能力用作实例级别的能力,那么您可以创建实例,但然后您要么需要将这些方法修改为实例级别的方法,要么需要通过类来访问它们。

英文:

A class is a set with abilities and properties. In terms of abilities/properties, you have two main kinds:

  • static (class-level)
  • instance-level

Now, a static method is the ability of the class and not of its instances. Since you defined initialize as a static method, it will not be reachable via instances of the class, as it's not an instance-level, but a class-level ability (method, to be precise). So, I have fixed your code by making sure that main1 and main2 are the exact classes of Main1 and Main2 respectively instead of instances and then their static methods can be reached. If you want to use instances and use these abilities as instance-level abilities, then you can create instances, but then you will either need to modify these methods to instance-level methods, or, you will need to reach them through the class.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

class Base{
    static val;
    static initialize(x){
        this.val = x;
    }
}
class Main1 extends Base{
    static showVal(){
        return this.val;
    }
}

class Main2 extends Base{
    static showVal(){
        return this.val * 2
    }
}


var main1 = Main1;
var main2 = Main2;

main1.initialize(1);
main2.initialize(2);

console.log(main1.showVal());// should show 1
console.log(main2.showVal());// should show 4

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月21日 01:49:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定