Javascript [Vue warn]:“TypeError:无法读取未定义的属性'type'?”
有人能帮我理解下面代码的行为吗 包装器属性{ 让properties=this.getToValueOf'wrapper',{} [properties['type'],properties['message']]=this.defineValidationState 返回属性 } 我得到以下警告: [Vue warn]:呈现函数中的错误:TypeError:无法读取未定义的属性“type” 当我在let关键字和destructuring赋值之间添加任何变量时,代码成功完成: 包装器属性{ 让properties=this.getToValueOf'wrapper',{},mysteriousVar [properties['type'],properties['message']]=this.defineValidationState 返回属性 } 为什么会发生这种情况?问题在于:Javascript [Vue warn]:“TypeError:无法读取未定义的属性'type'?”,javascript,ecmascript-6,vuejs2,webpack-2,Javascript,Ecmascript 6,Vuejs2,Webpack 2,有人能帮我理解下面代码的行为吗 包装器属性{ 让properties=this.getToValueOf'wrapper',{} [properties['type'],properties['message']]=this.defineValidationState 返回属性 } 我得到以下警告: [Vue warn]:呈现函数中的错误:TypeError:无法读取未定义的属性“type” 当我在let关键字和destructuring赋值之间添加任何变量时,代码成功完成: 包装器属性{ 让p
let properties = this.getToValueOf('wrapper', {})
[properties['type'], properties['message']]
被解释为:
let properties = this.getToValueOf('wrapper', {})[properties['type'], properties['message']]
因此,使用[]语法将其解释为属性访问,而不是解构
您应该在每条语句的末尾插入分号,或者真正阅读并理解standard.js编码标准。根据standard.js,必须在以{或[]开头的每一行的开头加上分号
我个人更喜欢经典的Douglas Crockford风格,所以我会在每句话的结尾加上分号。但无论哪种方式都有效:
Crockford风格:
wrapperProperties () {
let properties = this.getToValueOf('wrapper', {});
[properties['type'], properties['message']] = this.defineValidationState();
return properties;
}
Standard.js样式:
wrapperProperties () {
let properties = this.getToValueOf('wrapper', {})
;[properties['type'], properties['message']] = this.defineValidationState()
return properties
}