ES6 What does super() actually do in constructor function?

前端 未结 1 1525
粉色の甜心
粉色の甜心 2020-12-19 09:18

! Hola, amigos. I have this little class inheritance structure

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
    toStri         


        
相关标签:
1条回答
  • 2020-12-19 09:45

    super(…); is basically sugar for this = new ParentConstructor(…);. Where ParentConstructor is the extended class, and this = is the initialisation of the this keyword (well, given that that's forbidden syntax, there's a bit more than sugar to it). And actually it will inherit from the proper new.target.prototype instead of ParentConstructor.prototype like it would from new. So no, how it works under the hood does not compare to ES5 at all, this is really a new feature in ES6 classes (and finally enables us to properly subclass builtins).

    0 讨论(0)
提交回复
热议问题