【问题标题】:Difference between the super keyword of javascript and javajavascript和java的super关键字的区别
【发布时间】:2021-04-28 11:50:40
【问题描述】:

即使在使用 react 时,比如使用旧语法,我也认为 super 关键字会访问 Component 类的构造函数。 这是真的吗?

【问题讨论】:

  • 代码示例将非常有助于理解您想了解的内容。

标签: javascript java reactjs


【解决方案1】:

Java 和 JS 中的 super 关键字是对对象父级的引用。您可以使用 this 关键字访问/调用父级的构造函数、方法、属性。

class Cat extends Animal {
 
 Cat(){
  super(); // calls Animal's constructor
 }

 String toString() {
  return super.toString(); // calls toString() of Animal
 }
}

你用它做什么是有限制的,文档是你最好的朋友。

JS:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super

Java:https://docs.oracle.com/javase/tutorial/java/IandI/super.html

【讨论】:

    猜你喜欢
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 2018-05-30
    • 2021-10-03
    相关资源
    最近更新 更多