codecademy上面的题目,对象的属性名用字符串赋值,如何用所赋值来表示这个属性名?
关于JS的问题,在codecademy网站上遇到的,望各位帮忙解决。下面来介绍我遇到的问题:
Sometimes, beginners forget that object literal notation and dot notation assume that the value provided is a string. Take a look at the error here, and try to fix it by using bracket notation.
需求:The programmer wants to use the key 'weight' for the person's weight and store the person's weight in the weight variable.
提示:You can't use object literal notation to initialize the weight. Try using bracket notation for the weight.
person[WEIGHT_KEY] is the same as person['weight'].
You'll need to make two changes to the code.
程序代码:
// We're trying to use WEIGHT_KEY to avoid having to
// remember that the key that stores the weight information
// is called 'weight'.
var WEIGHT_KEY ='weight';
var person = {
name: 'Ryan',
WEIGHT_KEY:350
};
//var weight = person['weight'];