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

javascript - When do I need to call `super` from a constructor? - Stack Overflow

programmeradmin1浏览0评论

Reading Dr. Axel Rauschmayer's blog on ES6 classes, I understand that a derived class has the following default constructor when none is provided

constructor(...args) {
    super(...args);
}

I also understand that if I want to use this within a constructor I first need to call super, otherwise this will not yet be initialized (throwing a ReferenceError).

constructor(width, height) {
    this.width = width;  // ReferenceError
    super(width, height);
    this.height = height; // no error thrown
    ...
}

Is the following assumption then correct? (and if not, could you please explain the conditions under which I should explicitly call super)

For derived classes, I only need to explicitly call super when...

  1. I need to access this from within the constructor
  2. The superclass constructor requires different arguments then the derived class constructor

Are there other times when I should include a call to the superclass constructor?

Reading Dr. Axel Rauschmayer's blog on ES6 classes, I understand that a derived class has the following default constructor when none is provided

constructor(...args) {
    super(...args);
}

I also understand that if I want to use this within a constructor I first need to call super, otherwise this will not yet be initialized (throwing a ReferenceError).

constructor(width, height) {
    this.width = width;  // ReferenceError
    super(width, height);
    this.height = height; // no error thrown
    ...
}

Is the following assumption then correct? (and if not, could you please explain the conditions under which I should explicitly call super)

For derived classes, I only need to explicitly call super when...

  1. I need to access this from within the constructor
  2. The superclass constructor requires different arguments then the derived class constructor

Are there other times when I should include a call to the superclass constructor?

Share Improve this question edited Dec 27, 2016 at 19:18 Felix Kling 817k180 gold badges1.1k silver badges1.2k bronze badges asked Dec 27, 2016 at 19:16 sfletchesfletche 49.7k31 gold badges108 silver badges120 bronze badges 5
  • No, there are not. What kind of "proof" do you want for that statement? – Felix Kling Commented Dec 27, 2016 at 19:17
  • when you make a class like class ABC extends DEF { ... then you have to put super(...args);, because you just stated with extends DEF that you want to derive from it. If you don't derive from any class then just don't put 'super' in the ABC's constructor. It's that simple. – Azamantes Commented Dec 27, 2016 at 19:23
  • 1 @Azamantes: There is no need to call super if the parent class takes the same arguments as the child class. – Felix Kling Commented Dec 27, 2016 at 19:24
  • 1 what if the derived class does define a constructor? do i then need to include an explicit call to super? in other words, is the default call to super only included as part of the default constructor? (not sure of the use case here, just trying to think of edge cases) – sfletche Commented Dec 27, 2016 at 19:26
  • 1 @sfletche Yes, there is no "default super call" for all constructors, it's only part of the default constructor. – Bergi Commented Dec 27, 2016 at 19:29
Add a comment  | 

2 Answers 2

Reset to default 11

Yes, that sounds correct, albeit a bit oddly formulated. The rules should be

  • In a derived class, you always1 need to call the super(…) constructor
  • If you are not doing more than the default constructor, you can omit the whole constructor(){}, which in turn will make your class code not contain a super call.

1: You don't need to call it in the suspicious edge case of explicitly returning an object, which you hardly ever would.

You need to call super in a subclass constructor in these cases:

  • You want to reference this in the subclass constructor
  • You don't return a different object in the subclass constructor

In other cases, you can call it if you want the superclass constructor to run, but you don't have to.

class SuperClass{
  constructor() {
    console.log('SuperClass');
  }
}
class SubClass1 extends SuperClass {
  constructor() {
    console.log('SubClass1');
    super();
    return {};
  }
}
class SubClass2 extends SuperClass {
  constructor() {
    console.log('SubClass2');
    return {};
  }
}
new SubClass1();
new SubClass2();

I don't see how the order of arguments matters when deciding whether you should call super or not.

发布评论

评论列表(0)

  1. 暂无评论