Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
875 views
in Technique[技术] by (71.8m points)

为什么react的组件要super

为什么react的组件要supe


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

这是 ES6 语法中,子类调用超类的构造函数的方式。
如果以混合寄生方式实现继承:

// 超类
function Super(a){
    this.a = a;
}

// 子类
function SubClass(a){
    // 不执行这两句调用超类的构造函数的话,
    // 子类实例的 this.a 就是 undefined
    var super = Super.bind(this);
    super(a);
}
// ... 省略无关本题的代码

看起来是不是有内味了?ES6 规范当然也可以规定用其他方法来调用超类的构造函数,比如默认以子类的参数列表来调用,但这样一来,子类的参数列表就必须兼容超类的参数列表。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...