【问题标题】:How to access method of parent class from within a 'same name' method of the child class, using google glosure?如何使用 google glosure 从子类的“同名”方法中访问父类的方法?
【发布时间】:2014-05-23 13:23:59
【问题描述】:

我正在使用 google 闭包构建 API。

我想从子类 B 中具有同名的方法中访问超类 A 的方法。

请看下面的伪代码:

超类 A

class A {
    move: function() { ... }
}

儿童B班

class B extends A {
    // B has its own 'move' method which uses the 'move' method from A
    move: function() {
        parentClass_.move();
    }
}

我在某处读到关键字“parentClass”就是这样做的。 我读到here 是关键字“superClass_”这样做的。

它们都不起作用。也许我做错了。

有人可以帮帮我吗?

谢谢。

若昂

【问题讨论】:

标签: javascript class methods google-closure-compiler


【解决方案1】:

这取决于类的定义方式。您在问题中使用的语法与我所知道的都不匹配。

最终,您需要对超类的引用,并且该值的定义方式必须使子类型定义不会覆盖它。通常,这是通过原型对象完成的(但还有其他方式),在这种情况下您可以这样做:

A.prototype.move.call(this)

如果这是闭包库代码,你可以使用:

B.base(this, 'move');

【讨论】:

    猜你喜欢
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 2018-10-21
    相关资源
    最近更新 更多