Javascript对象语法声明函数时出错

时间:2017-09-15 16:11:19

标签: javascript

我一直得到:

SyntaxError: Unexpected identifier 'drive'. Expected '}' to end an object literal. (line 9)

似乎建议额外的'}',但我无法发现它,或者弄清楚是否还有其他错误。我是JavaScript的新手,所以任何帮助将不胜感激。请参阅下面的代码。

谢谢!

var taxi = {
    make: "Toyota",
    model: "Taxi",
    year: 1955,
    colour: "yellow",
    passengers: 4,
    convertible: false,
    mileage: 281341
    drive: funtion() {
        alert("Voom Voom");
    }
};

function prequal(car) {
    if (car.mileage > 10000 || car.year > 1960) {
        return false;
    }
    return true;
}

var worthALook = prequal(taxi);

if (worthALook) {
    console.log("Check it out! " + taxi.make + " " + taxi.model);
}
else {
    console.log("Not worth! " + taxi.make + " " + taxi.model);  
}

taxi.drive();

1 个答案:

答案 0 :(得分:3)

您在第8行中遗漏了一个逗号,将taxi.drivevar taxi = { make: "Toyota", model: "Taxi", year: 1955, colour: "yellow", passengers: 4, convertible: false, mileage: 281341, drive: function() { alert("Voom Voom"); } }; function prequal(car) { if (car.mileage > 10000 || car.year > 1960) { return false; } return true; } var worthALook = prequal(taxi); if (worthALook) { console.log("Check it out! " + taxi.make + " " + taxi.model); } else { console.log("Not worth! " + taxi.make + " " + taxi.model); } taxi.drive();分开。你还拼写了“功能”#39;第9行不正确。

解决这些问题,似乎工作正常:



if @FLAG then
{  
   multi_bin;
}
else
{
}