我剛剛開始學習模塊化設計原則,並且我相信我不瞭解我的一種方法的上下文。在控制檯中,我得到Uncaught TypeError: Cannot read property 'val' of undefined - line 19
。如果有問題,我正在使用Firebase。JS模塊化設計 - 上下文問題
這裏是我的代碼:
(function(){
var UpdateTable = {
resources: Resources,
init: function(){
this.cacheDom();
this.render();
this.update(snapshot); // Changed this line ///
},
render: function(){
this.resources.on('value', this.update);
},
cacheDom: function(){
this.$table = $('#resourceTable');
},
update: function(snapshot){
console.log(snapshot.val());
for(var resource in this.resources){
this.$table.append('<tr><td>' + this.resources[resource].name + '</td><td>' + this.resources[resource].language + '</td></tr>');
}
}
};
UpdateTable.init();
})();
當您從init方法調用更新時,不會傳遞任何東西作爲快照參數。 –