查看“︁JavaScript/super”︁的源代码
来自康健生活
←
JavaScript/super
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 引言 == == constructor == {{hcdiff|head=语法糖|output=<syntaxhighlight lang="javascript"> function Point(x, y) { this.x = x; this.y = y; } Point.prototype.toString = function () { return `(${this.x},${this.y})`; }</syntaxhighlight>|output2=<syntaxhighlight lang="javascript"> class Point { constructor(x ,y) { this.x = x; this.y = y; } toString() { return `(${this.x},${this.y})`; } }</syntaxhighlight>}} == super == 子类的构造函数中必须执行一次 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 { constructor() { console.log('class A'); } } class B extends A { constructor() { super(); } } new A(); new B();</syntaxhighlight>|{{consoleout|<pre>class A class A </pre>}}}} {{Double-columns|<syntaxhighlight lang="javascript"> class A { foo() { return 'kangkk'; } } class B extends A { constructor() { super(); console.log(super.foo()); } } const bar = new B(); console.log(bar);</syntaxhighlight>|{{consoleout|<pre>kangkk B {} </pre>}}}}
此页面嵌入的页面:
模板:Consoleout
(
查看源代码
)
模板:Double-columns
(
查看源代码
)
模板:Double-columns/style.css
(
查看源代码
)
模板:Hcdiff
(
查看源代码
)
模板:Hcdiff/style.css
(
查看源代码
)
返回
JavaScript/super
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。