Lin*_*nas 2 javascript javascript-objects
我不明白为什么我不能访问这样的值:
object = {
test:{
value: "Hello world"
}
}
variable = "value";
//this gives me "Hello world"
console.log(object.test.value);
//this gives me undefined error
console.log(object.test.variable);
Run Code Online (Sandbox Code Playgroud)
到现在为止我可以理解它不能以这种方式完成,但我仍然需要为变量赋予一些值,然后使用该变量来访问对象值.
Jon*_*n M 11
这样做:
console.log(object.test[variable]);
Run Code Online (Sandbox Code Playgroud)
用点做它是使用文字属性名称.即,object.test.value相当于object.test['value'].