我有這個對象結構並想遍歷obj
的所有直接子對象並調用它們的myMethod
方法。 雖然for...in
遍歷他們正確的,我總是得到這個錯誤o.myMethod is not a function
在類似數組的對象中迭代和調用方法
這裏是一個JSFiddle
obj = {
test1: {
"name": "test1string",
"myMethod": function(){
console.log("test 1 method called")
}
},
test2: {
"name": "test2string",
"myMethod": function(){
console.log("test 2 method called")
}
}
};
for (var o in obj) {
console.log(o.name());
o.myMethod();
}
我怎樣才能達到想要的行爲?