JavaScript/super:修订间差异
来自康健生活
创建页面,内容为“== 引言 == == constructor == {{hcdiff|head=语法糖|output=<syntaxhighlight lang="javascript"> function Point(x, y) { this.x = x; this.y = y; } Point.prototyp…” |
|||
| 第20行: | 第20行: | ||
== super == | == super == | ||
子类的构造函数中必须执行一次 super 函数,子类没有自己的 this 对象,而是继承父类的 this 对象。 <code>super()</code> 等价于 <code>A.prototype.constructor.call(this, props)</code>。 | |||
<div class="pageBoxGreen">子类通过 super 调用父类方法,super 会绑定子类的 this。</div> | |||
{{leftrightflex|<syntaxhighlight lang="javascript"> | {{leftrightflex|<syntaxhighlight lang="javascript"> | ||
class A { | class A { | ||