最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ES6: this within static method - Stack Overflow

programmeradmin1浏览0评论

Let's say I have two ES6 classes like this:

class Base {
    static something() {
        console.log(this);
    }
}

class Derived extends Base {
}

And then I make a call like this:

Derived.something();

Note that I am making a call to a static method defined on the super class via sub class.

This does not give me errors. It prints

[Function: Derived]

So accessing this within a static method seems to work here.

I need a common static method for all sub-classes of a super class and I need to be able to know what sub-class is calling this method.

Now my question is whether using this within a static method is legal. I know these static methods become class methods, and hence this would naturally point to the class object they are called on. (The class object being the constructor.)

But I can't seem to find any definitive resource that states that this is allowed by the ES specification.

This looks like a good introduction to ES6 classes but does not talk about this with static.

Let's say I have two ES6 classes like this:

class Base {
    static something() {
        console.log(this);
    }
}

class Derived extends Base {
}

And then I make a call like this:

Derived.something();

Note that I am making a call to a static method defined on the super class via sub class.

This does not give me errors. It prints

[Function: Derived]

So accessing this within a static method seems to work here.

I need a common static method for all sub-classes of a super class and I need to be able to know what sub-class is calling this method.

Now my question is whether using this within a static method is legal. I know these static methods become class methods, and hence this would naturally point to the class object they are called on. (The class object being the constructor.)

But I can't seem to find any definitive resource that states that this is allowed by the ES specification.

This looks like a good introduction to ES6 classes but does not talk about this with static.

Share Improve this question asked May 11, 2018 at 4:57 treecodertreecoder 45.1k23 gold badges70 silver badges97 bronze badges 1
  • 2 this works exactly like in any other non-arrow function: It's value depends on how the function is called. A static class method is a function like any other. That's probably the reason why you don't find anything specific about it. – Felix Kling Commented May 11, 2018 at 5:11
Add a comment  | 

3 Answers 3

Reset to default 10

Under typical circumstances, the this in any call to something.method() will refer to something as long as the function is not an arrow function, bound function, or something like that (and it is neither of those in this case).

Class inheritance, or even ES6, aren't really relevant here. All you need to know is that you are calling Derived.something(), so this will refer to Derived.

Yes, this is legal in static methods, that's the way this should be done.

this refers to class instance in prototype methods and refers to class constructor in static methods, unless a method was unbound from its original context.

Similarly, super refers to parent class prototype in instance methods and refers to parent class constructor in static methods.

As long as the static method is invoked as a member expression, e.g.

Derived.something();

as opposed to

const { something } = Derived;
something();

then this will refer to Derived. Derived.something() is identical to something.call(Derived) if Derived.something is stored to an intermediate variable, because that's how a member expression with a nested call expression is evaluated, essentially.

发布评论

评论列表(0)

  1. 暂无评论