我得到的错误对象是不确定的,当我把它从另一个对象
Uncaught TypeError: Cannot read property 'render' of undefined
它是在第29行,当我调用该函数listView.render()
感谢您的帮助
var model = {
// the array with cats names
cats: ['mew', 'meo', 'meaw', 'memaw', 'mesaw', 'metaw'],
// The array of cats objects
Cats: []
}
var oct = {
// selecting the list on the left of the container
catsList: function() {
return document.querySelector('#catsList');
},
//selecting the cat display div
catsDisplay: function() {
return document.querySelector('#catsDisplay');
},
// creating the Cat object and push it to the Cats array
addCat: (function (arr) {
for (var i = 0; i < arr.length; i++) {
model.Cats.push((function (n) {
return {
name: n,
photo: n + '.jpg',
score: 0
};
}(arr[i])));
listView.render(model.Cats[i]);
};
}(model.cats))
}
var listView = {
render: function (CatObj) {
oct.CatList.innerHTML = "<div id='"+CatObj.addCat.name+"'>ss</div>"
}
}
的'addCat'代码被包装在一个IIFE并且因此执行的时候了。这是一个非常糟糕的设计,并且非常出乎意料,也是为什么'listView'在那个时候是未定义的。你至少应该有一些像'oct.init()'这样的东西。你的整个代码实际上是非常糟糕的海事组织(没有冒犯性),在各地都有紧密的耦合和隐藏的依赖。例如,为什么listView没有封装它自己的列表DOM元素......你不必从那里到达oct。 – plalx