!DOCTYPE htmlhtml head meta charset=UTF-8 titletitle head body script class Father constructor thisx=x; thisy=y; sum consolelogthisx+thisy class Son extends Father c
<p>代码没有明显的语法错误,但是在Father类的构造函数中,this.x和this.y是没有被定义的。也就是说,构造函数应该接收x和y作为参数,并将它们赋值给this.x和this.y。修改后的代码如下:</p>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
class Father{
constructor(x, y){
this.x = x;
this.y = y;
}
<pre><code> sum(){
console.log(this.x + this.y)
}
}
class Son extends Father{
constructor(x, y){
</code></pre>
<p>// this.x=x;
// this.y=y;
// 调用父类的构造方法,将参数x,y传给了父类的constructor
super(x, y)
}
}</p>
<pre><code> var son = new Son(1, 2);
son.sum();
</script>
</body>
</code></pre>
</html
原文地址: https://www.cveoy.top/t/topic/ftr2 著作权归作者所有。请勿转载和采集!