是said ES6 class
关键字就是 syntactic sugar Javascript 基于原型(prototype)的继承系统。这是显而易见的,但有一个异常(exception):extends
关键字。例如:
class Foo {
constructor(){
this.sayHello = () => console.log('Hello')
}
}
class Bar extends Foo {
constructor(){
super();
this.sayGoodbye = () => console.log('Goodbye');
}
}
如果class
只是语法糖,那么这里到底发生了什么?是否有一个标准的
最佳答案
Is there a standard < ES6 design pattern that simulates class inheritance being invoked?
JavaScript 有一个原型(prototype)继承模型,这也是通过 class
关键字得到的。您的代码大约相当于:
function Foo () {
this.sayHello = () => console.log('Hello')
}
function Bar () {
// super();
Foo.call( this );
this.sayGoodbye = () => console.log('Goodbye');
}
// extends
Bar.prototype = Object.create( Foo.prototype );
Bar.prototype.constructor = Bar;
关于javascript - 非 ES6 Javascript 中如何处理类扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48100108/