我用javascript编写了以下代码:
class Screen {
Screen(_width, _height){
this.width=_width;
this.height=_height;
}
diagonal() {
return Math.sqrt(Math.pow(this.width, 2) + Math.pow(this.height, 2));
}
dimensions(definition) {
var dimensions = definition.split('x')
this.width = parseInt(dimensions[0]);
this.height = parseInt(dimensions[1]);
}
}
var screen = new Screen(0, 0);
screen.dimensions = '500x300';
screen.width = 400;
console.log(screen.diagonal);
当我调用 screen.diagonal 时,它不会返回结果和对角线
函数没有被调用。这可能是什么原因?
理想情况下,函数调用应该返回 500
答案 0 :(得分:0)
删除函数参数周围的引号。即 ('500x300') 应该是 (500x300)。