我有三个我期望的输出(第二个测试)中的一个,但无法弄清楚如何得到另外两个。我也包含下面的测试代码。我是否需要添加属性才能生成所需的输出?
function car(gas, mpg) {
this.gas = gas;
if (this.gas <= 0) {
this.empty = true;
}
this.mpg = mpg;
//this.range = gas * mpg;
//this.empty = empty;
this.drive = function(miles) {
this.gas -= miles/this.mpg;
if (this.gas <= 0) {
this.empty = true;
}
};
this.fill = function(gallons) {
if (this.gas <= 0) {
this.fill = false;
}
};
}
//test code
var test = new car(15, 30);
console.log(test.empty); //expected output = false
test.drive(500);
console.log(test.empty); //expected output = true
test.fill(20);
console.log(test.empty); //expected output = false
该代码中没有数组。 –
我已将您的代码移至* runnable * Stack Snippet,并将'console.log'替换为'alert'。 –