我是编程新手。我下面的javascript代码没有将我的area值渲染为4,因为我没有声明相同的值。有人请告诉我应该在哪里声明height和width的值?
class polygon {
constructor(){
this.name= "polygon";
}
}
var object= new polygon();
console.log(object.name);
class square extends polygon {
constructor(length) {
super(length,length);
this.name = 'Square';
}
get area() {
return this.height * this.width;
}
set area(value) {
this.value = area;
}
}
var obj= new square();
var x = new area(2,2);
console.log(obj.name);
console.log(x);
答案 0 :(得分:1)
尝试一下。 如果要将高度和宽度作为单独的变量,则应将其包括在多边形中,并为正方形分配相同的高度和宽度。
class polygon {
constructor(height, width) {
this.name = "polygon";
this.height = height;
this.width = width;
}
}
var object = new polygon();
console.log(object.name);
class square extends polygon {
constructor(length) {
super(length, length);
this.name = "Square";
}
get area() {
return this.height * this.width;
}
set area(value) {
this.value = area;
}
}
var obj = new square();
var x = new square(2);
console.log(x.name);
console.log(x.area);