0
$(document).ready(function() {
function Group(label, children) {
this.label = ko.observable(label);
this.children = ko.observableArray(children);
}
function getGroups() {
var grps = [
[new Group("Group 1", [])],
[new Group("Group 2", [])]
];
for (var a in grps) {
alert(a.toString()); // works, alerts index
alert(a.label()); // doesn't works. should give Group label
}
return grps;
}
getgroups();
});
當我嘗試調用getGroups()函數警報不起作用。我不知道似乎是什麼問題。無法從JavaScript類獲得價值
現在它只顯示未定義。 – Ruchan
哦,只是注意到你正在將數組插入到grps數組中。你需要使用'alert((grps [a])[0] .label)' – closure
哦,現在我明白了...爲什麼它不工作。這是因爲'this'修飾符實際上不是'Group'的文檔。 – Ruchan