从父级调用覆盖的静态方法 [英] Calling an overridden static method from parent

查看:69
本文介绍了从父级调用覆盖的静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,提供一些代码来设置阶段:

First, some code to set the stage:

var instances = [];

class Parent {
    static doImportantStuff() {
        console.log( 'Parent doing important stuff' );
        return false;
    }

    static init() {
        if ( this.doImportantStuff() )
            instances.push( new this() );
    }
}

class Child1 extends Parent {
    static doImportantStuff() {
        console.log( 'Child 1 doing important stuff' );
        if (someCondition) return true;
        return false;
    }
}

class Child2 extends Parent {
    static doImportantStuff() {
        console.log( 'Child 2 doing important stuff' );
        if (someOtherCondition) return true;
        return false;
    }
}

这个想法是存在一个Parent类,它扩展了几个Child类. Child类的初始化几乎是相同的,但每个类都有自己的doImportantStuff()实现,其返回值指示是否应实例化特定的Child.

The idea is that there's a class Parent that several Child classes extend. Initialization of the Child classes is mostly the same, but each has their own implementation of doImportantStuff(), the return value of which indicated whether that particular Child should be instantiated.

到目前为止,这在我尝试过的每个编译器中都有效,因为Parent.init()函数中的this引用了Child类的构造函数.但是我还没有找到关于引用被子类覆盖的静态方法的任何一种或另一种文档,所以问题是,我可以一直这样吗?或者,还有其他方法吗?

So far this has worked in each transpiler I've tried, because this in the Parent.init() function refers to the constructor of the Child class. However I haven't found any documentation saying one way or the other about referring to a static method overridden by a child class, so the question is, can I rely on this always being so? Or, is there some other way to do this?

推荐答案

但是,我还没有找到任何文档来提及被子类覆盖的静态方法的一种或另一种方式,所以问题是,我是否可以一直依靠这种方式?

However I haven't found any documentation saying one way or the other about referring to a static method overridden by a child class, so the question is, can I rely on this always being so?

这是标准的通过对象调用属性"功能.当您这样做时:

It's the standard function-call-via-object-property mechanism. When you do:

Child1.doImportantStuff();

......除非doImportantStuff是箭头函数(不是)或绑定函数(不是),然后在调用过程中,this设置为Child1.完全一样:

...unless doImportantStuff is an arrow function (it isn't) or a bound function (it isn't), then during the call, this is set to Child1. Exactly like:

var obj = {
   foo: function() {
        console.log(this === obj);
   }
};
obj.foo();   // "true"

是的,您可以依靠它. (而且我知道您为什么要问,除非您逐步解决,否则它确实似乎有点奇怪.)

So yes, you can rely on that. (And I understand why you asked, it does seem a bit odd unless you work it through.)

当然,它不能在非static函数的代码中运行,因为this将引用实例,而不是构造函数.如果在那里需要它,则可以使用this.constructor.doImportantStuff,除非有人弄乱了constructor属性. (人们总是习惯把它弄乱;借助新的语法将其自动化,尽管很少有人真正需要它,希望这种情况会减少.)

Of course, it won't work from within the code of a non-static function, because this will refer to the instance, not the constructor function. If you needed it there, you could use this.constructor.doImportantStuff unless someone's messed up the constructor property. (People always used to mess it up; with the new syntax automating it, hopefully that will happen less although it's rare you really need it...)

对于这些类型的问题,经常记住新类语法几乎是 语法,这对于我们做旧的冗长方式(如果我们真的很彻底)而言.它是非常好的"糖,但几乎就够了(这是一件好事™). static方法设置为构造函数的属性,非static方法设置为构造函数的prototype属性上对象的属性. (我认为它唯一的非糖方面是,如 Bergi 所指出的那样,新语法使我们可以扩展内置函数.就像以前不可能做到的Array一样,实现这一目标的一部分与this的设置方式和时间有关[您无法在子类构造函数中调用super()之前对其进行访问",其中与new.target有关,Bergi 在此处进行讨论.在ES7中,它可能不仅仅包含一些具有隐私性的内容.)

For these kinds of questions, it's frequently useful to remember that the new class syntax is almost just syntactic sugar for the old verbose way we did it (if we were really thorough). It's really good sugar, but that's nearly all it is (and that's a Good Thing™). static methods are set as properties of the constructor function, non-static methods are set up as properties of the object on the constructor's prototype property. (I think the only non-sugar aspect of it is, as Bergi points out, that the new syntax lets us extend builtins like Array which it wasn't possible to do before. Part of making that possible relates to how and when this gets set up [you can't access it prior to the super() call in your subclass constructor], which relates to new.target, which Bergi discusses here. In ES7, it may go further beyond sugar with privacy stuff.)

这篇关于从父级调用覆盖的静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆