我在函数中将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
变量。任何帮助将不胜感激。
答案 0 :(得分:6)
您不能使用var
语句声明对象的属性,因为var
语句要求变量名称不包含无效字符。 .
绝对是标识符名称的无效字符。因此,您无法声明名为reason.reasons
或reason.scholarships
的新变量。
您应该像这样声明reason
var reason = {
reasons: [],
scholarships = []
};
答案 1 :(得分:2)
如同thefourtheye所说,你可以这样做:
var reason = {
reasons: [],
scholarships = []
};
或者如果你想单独完成所有操作,你可以这样做:
var reason = {};
reason.reasons = [];
reason.scholarships = [];