Jshint - variable is already defined and javascript object semicolon is missing

I have defined a variable reason

as a javascript object inside a function and then use that object to define properties like this:

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

      

I don't know why I always had 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 have not defined a variable reason

elsewhere in the code. Any help would be appreciated.

+3


source to share


2 answers


You cannot declare a property of an object with an expression var

, because the operator var

expects variable names to not contain invalid characters. And is .

definitely an invalid character for an identifier name. Thus, you cannot declare a new variable named reason.reasons

or reason.scholarships

.

You should declare reason

like this



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

      

+6


source


As thefourtheye , you can do this:

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

      



Or, if you want to do it all separately, you can do:

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

      

+2


source







All Articles