屬性我知道JS對象的獲取值可以這樣做:JavaScript不能得到與obj.x
obj1.x
或
obj1[x]
但下面的例子obj1.x
中是行不通的。爲什麼?
var txt = "";
var person = {fname:"John", lname:"Doe", age:25};
for (x in person) {
txt += person[x] + " ";//this is working
txt += person.x + " ";//this is not working
}
這表明,x爲屬性的名稱:
for (x in person) {
txt += x;//output: fname, lname
}
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_accessors – zerkms