我最近开始学习Classesinjavascript并在阅读一些非常有趣的东西时,我想尝试一些我自己的想法。
如果你有一个父类Parent中,你有一个method的logSomething```` and a child class of子, with which you do类儿童家长延伸, how can you then execute the inherited method from the parent class,logSomething```,子类的里面?
如果在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。
怎么做呢?
回首忆惘然
白猪掌柜的
至尊宝的传说
相关分类