【发布时间】:2020-01-14 09:27:40
【问题描述】:
我最近开始在javascript 中了解Classes,在阅读一些非常有趣的东西时,我想尝试一些自己的想法。
如果你有一个Parent 的父类,其中你有一个logSomething```` and a child class ofChild, with which you doclass Child extends Parent, how can you then execute the inherited method from the parent class,logSomething 的method,在子类里面?
如果您在Child 类中定义一个方法并将this.logSomething() 添加到该方法中,则每当调用子类中的方法时,继承的logSomething 函数确实会运行,但除此之外我没有'没有找到任何直接在该子类中执行 logSomething 的方法。
我已经尝试过this.logSomething(),我已经尝试将它添加到一个对象、自执行 (IIFE) 函数以及我可以做的所有事情但没有结果。
class Parent {
constructor() {}
logSomething() {
console.log('I am logging something')
}
}
class Child extends Paren {
logSomething() // This does not work
}
目前这样做是行不通的,如果抛出一个错误,指的是你试图定义一个函数。
如果我没弄错React 使用与life-cycle methods 类似的东西,我知道这应该是可能的,对吧?如componentWillMount。
要怎么做呢?
【问题讨论】:
标签: javascript es6-class