gpt4 book ai didi

JavaScript:用于检索属性值的括号表示法

转载 作者:行者123 更新时间:2023-12-02 14:50:02 24 4
gpt4 key购买 nike

我正在编写 codecademy.com JavaScript 教程。这节课是关于物体的。我感觉这个问题相当简单,但我没有得到答案。如果我更好地理解说明,答案对我来说可能会更清楚。

我将值设置为变量 aProperty,现在我应该遵循最后注释中的说明,即使用变量“aProperty”打印第一个属性的值。我在下面添加了“类(class)简介”,以帮助解释本类(class)试图教授的内容。

问题:假设我正确设置了变量 aProperty,那么如何使用变量 aProperty 检索 James 对象的第一个值。

var james = {
job: "programmer",
married: false
};

// set to the first property name of "james"
var aProperty = james.job;

// print the value of the first property of "james"
// using the variable "aProperty"

类(class)简介

And finally, let's go over retrieving property values. Throughout this section, we've been using dot notation to get the value of an object's property:

someObj.propName

However, remember that we can also use bracket notation:

someObj["propName"]

An advantage of bracket notation is that we are not restricted to just using strings in the brackets. We can also use variables whose values are property names:

var someObj = {propName: someValue}; var myProperty = "propName"; someObj[myProperty]

The last line is exactly the same as using someObj["propName"].

Take advantage of the ability to use variables with bracket notation.

In line 7, set aProperty to a string of the first property in james (ie. the job property).

Then print james's job using bracket notation and aProperty.

最佳答案

var aProperty = 'job';
console.log(james[aProperty]);

关于JavaScript:用于检索属性值的括号表示法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12150696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com