JavaScript/super:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
创建页面,内容为“== 引言 == == constructor == {{hcdiff|head=语法糖|output=<syntaxhighlight lang="javascript"> function Point(x, y) { this.x = x; this.y = y; } Point.prototyp…”
 
Kangkk​(留言 | 贡献)
小 文字替换 -“{{leftrightflex|”替换为“{{Double-columns|”
 
(未显示同一用户的1个中间版本)
第20行: 第20行:


== super ==
== super ==
{{leftrightflex|<syntaxhighlight lang="javascript">
子类的构造函数中必须执行一次 super 函数,子类没有自己的 this 对象,而是继承父类的 this 对象。 <code>super()</code> 等价于 <code>A.prototype.constructor.call(this, props)</code>。
 
<div class="pageBoxGreen">子类通过 super 调用父类方法,super 会绑定子类的 this。</div>
 
{{Double-columns|<syntaxhighlight lang="javascript">
class A {
class A {
   constructor() {
   constructor() {
第37行: 第41行:




{{leftrightflex|<syntaxhighlight lang="javascript">
{{Double-columns|<syntaxhighlight lang="javascript">
class A {
class A {
   foo() {
   foo() {