Jshint - 变量已经定义并且缺少javascript对象的分号

时间:2014-12-15 04:32:54

标签: javascript jshint

我在函数中将reason变量定义为javascript对象,然后使用此对象定义属性如下:

$scope.upsertReason = function() {
    var reason = {},
        reason.reasons = [],
        reason.scholarships = [];
}

我不知道为什么我总是遇到以下错误:

412 |            reason.reasons = [],
                       ^ 'reason' is already defined.
412 |            reason.reasons = [],
                       ^ Missing semicolon.
412 |            reason.reasons = [],
                       ^ Expected an identifier and instead saw '.'.
412 |            reason.reasons = [],
                       ^ Expected an assignment or function call and instead saw an expression.
412 |            reason.reasons = [],
                        ^ Missing semicolon.
413 |            reason.scholarships = [];
                                        ^ Expected an assignment or function

我已经确认我没有在代码中的任何其他地方定义reason变量。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:6)

您不能使用var语句声明对象的属性,因为var语句要求变量名称不包含无效字符。 .绝对是标识符名称的无效字符。因此,您无法声明名为reason.reasonsreason.scholarships的新变量。

您应该像这样声明reason

var reason = {
    reasons: [],
    scholarships = []
};

答案 1 :(得分:2)

如同thefourtheye所说,你可以这样做:

var reason = {
    reasons: [],
    scholarships = []
};

或者如果你想单独完成所有操作,你可以这样做:

var reason = {};
reason.reasons = [];
reason.scholarships = [];