2016-02-26 23 views
3

我得到的错误对象是不确定的,当我把它从另一个对象

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>" 
    } 
} 
+0

的'addCat'代码被包装在一个IIFE并且因此执行的时候了。这是一个非常糟糕的设计,并且非常出乎意料,也是为什么'listView'在那个时候是未定义的。你至少应该有一些像'oct.init()'这样的东西。你的整个代码实际上是非常糟糕的海事组织(没有冒犯性),在各地都有紧密的耦合和隐藏的依赖。例如,为什么listView没有封装它自己的列表DOM元素......你不必从那里到达oct。 – plalx

回答

0

listView被吊起并设置为undefined。所以在这里 listView.render(model.Cats[i]);listViewundefined

+0

这是真的,但真正的问题是IIFE。 – plalx

0

声明之前,您不能使用的ListView。试试这个:当你声明作为变量

var model = { 
    // the array with cats names 
    cats: ['mew', 'meo', 'meaw', 'memaw', 'mesaw', 'metaw'], 
    // The array of cats objects 
    Cats: [] 
    } 


var listView = { 
    render: function (CatObj) { 
     //oct.CatList.innerHTML = "<div id='"+CatObj.addCat.name+"'>ss</div>" 
    } 
} 


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>" 
    } 
} 
+3

通过这个概念,现在'oct'不会被定义吗? – Brett

+0

@布雷特啊,谢谢你,我会改变答案。 –

0

,他们在他们的地方要建立这样在这种情况下你声明的ListView你调用它

如果你想ListView控件是后可用,不管它在哪里更改为常规功能,例如

function listView() { 

} 

由于函数将在运行时被悬挂到顶部。

0

似乎没有被一个酒店在oct对象命名为CatList。你也没有调用这个函数。尝试使用oct.CatListoct.catsList,调用函数。也改变Cats_Cats,用返回一个可能被调用的函数替换IIFE

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) { 
 
    return function() { 
 
     for (var i = 0; i < arr.length; i++) { 
 
     model._Cats.push({ 
 
      name: i, 
 
      photo: i + '.jpg', 
 
      score: 0 
 
     }); 
 
     listView.render(model._Cats[i]); 
 
     }; 
 
    } 
 
    }(model.cats)) 
 
} 
 

 
var listView = { 
 
    render: function(CatObj) { 
 
    oct.catsList().innerHTML += "<div id='" + CatObj.name + "'>ss</div>" 
 
    } 
 
} 
 

 
oct.addCat()
<div id="catsList"> 
 

 
</div> 
 

 
<div id="catsDisplay"> 
 

 
</div>

https://jsfiddle.net/8u60bcsv/1/的jsfiddle

相关问题