ES6 - 构造函数中需要超级 [英] ES6 - Super required in constructor
问题描述
class Overflow {}
class Stack extends Overflow {
constructor(){
super();
}
}
let stack = new Stack();
https://plnkr.co/edit/JqRfuDAav9opvapwCGpT?p=preview
如果我使用 construtor ()
我必须调用super()始终。
为什么不 c code code code code code code code code $ c $ code>这里调用基础构造函数
AND 设置原型?似乎错了
编辑:为什么需要 super()
?即使我没有打算调用基础构造函数。
Axel FTW
http://www.2ality.com/2015/02/es6-classes-final.html
摘要
为什么需要超级用户?
- 新关键字
class
不仅仅是语法糖。 - 仅发生分配和实例化在基础构造函数中。
但是为什么这样?
- 允许异域对象扩展(谢谢Felix Kling)。
class Overflow {}
class Stack extends Overflow {
constructor() {
super();
}
}
let stack = new Stack();
https://plnkr.co/edit/JqRfuDAav9opvapwCGpT?p=preview
If I use construtor()
I must call super() always.
Why isn't super()
auto called in constructor?
Edit: Is super()
here calling the base constructor
AND setting the prototype? Seems wrong.
Edit: Why is super()
required? Even when I've no intention of calling the base constructor.
Axel FTW http://www.2ality.com/2015/02/es6-classes-final.html
Summary
Why super is required?
- The new keyword
class
isn't just syntax sugar. - Allocation and instantiation only happens in the base constructor.
But why this?
- To allow exotic objects to be extended (thank you Felix Kling).
这篇关于ES6 - 构造函数中需要超级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!