var person1 = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
很多对象就像上面那样。 我试过得到像下面的对象,但没有工作。
var id = "1";
person+id.firstName + " is " + person+id.age + " years old.";
请指导我。
答案 0 :(得分:1)
如果它在全局上下文中,那么您可以从window
对象
var person1 = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
var id = "1";
document.write(window["person" + id].firstName + " is " + window["person" + id].age + " years old.");