简体   繁体   English

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

[英]Jshint - Variable is already defined and missing semicolon of a javascript object

I defined a reason variable as javascript object inside a function, and then, use this object to define properties as following: 我在一个函数中将一个reason变量定义为javascript对象,然后使用该对象定义属性如下:

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

I don't know why I always got the following error: 我不知道为什么我总是遇到以下错误:

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

I have verified that I didn't define the reason variable anywhere else in the code. 我已经验证我没有在代码中的任何其他地方定义reason变量。 Any help would be appreciated. 任何帮助,将不胜感激。

You cannot declare a property of an object with var statement, because var statement expects the variable names not to contain invalid characters. 您不能使用var语句声明对象的属性,因为var语句要求变量名称不包含无效字符。 And . 并且. is definitely an invalid character for an identifier name. 绝对是标识符名称的无效字符。 So, you cannot declare a new variable called reason.reasons or reason.scholarships . 因此,您无法声明名为reason.reasonsreason.scholarships的新变量。

You should declare reason like this 你应该声明reason

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

Like thefourtheye said, you can either do this: 就像thefourtheye所说,你可以这样做:

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

Or if you want to do it all separately, you can do: 或者,如果您想单独完成所有操作,您可以:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM