codecademy上面的题目,对象的属性名用字符串赋值,如何用所赋值来表示这个属性名?

liwenxianshifu 2011-11-01 05:34:18
关于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'];
...全文
145 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
liwenxianshifu 2011-11-02
  • 打赏
  • 举报
回复
早上试着再做一次,居然让我成功了。下面是修改后的程序代码:
// 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:350
};

var weight = person['weight'];


将person对象属性WEIGHT_KEY改成weight,我想之前我一直做不出来很大部分原因是没有理解英文需求的意思,英文水平不高是一个很大的问题啊!
不过,问题解决了,我真的很开心!哇哈哈哈!

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧