我得到了错误
Uncaught TypeError: Cannot read property 'render' of undefined
当我调用函数 listView.render()
时,它在第 29 行
感谢帮助
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>"
}
}
最佳答案
listView
正在被提升并设置为 undefined
。所以在这里
listView.render(model.Cats[i]);
, listView
是 undefined
关于javascript - 当我从另一个对象调用它时对象未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35660039/