var WEIGHT_KEY= 'weight';
var person{
name: 'Ryan'
WEIGHT_KEY: 350
};
var weight = person;WEIGHT_KEY
应该是这样的:
var WEIGHT_KEY= 'weight';
var person = { name: 'Ryan' };
person[WEIGHT_KEY] = 350;
var weight = person[WEIGHT_KEY];
JavaScript 对象的行为类似于属性包,因此您可以像索引器一样使用属性名称来获取和设置。
缺少等号,即对象属性和对象属性表示法的句点之间的逗号。
var WEIGHT_KEY= 'weight';
var person = { name: 'Ryan', WEIGHT_KEY: 350 };
var weight = person.WEIGHT_KEY;
方括号法:
var person = {};
person['name'] = 'Ryan';
person[WEIGHT_KEY] = 350;