Backbone.js 如何识别未定义的内容

Backbone.js 如何识别未定义的内容,backbone.js,Backbone.js,我在主干应用程序中遇到一个未定义的错误。我不知道什么是未定义的。在路由器的索引函数中,我有 var view = new Enki.Views.EntriesIndex(); console.log(view); $('#container').html(view.render().el); 这最后一行给了我这个错误 未捕获的TypeError:无法读取未定义的属性“el” console.log(view)显示view=new Enki.Views.EntriesIndex()。

我在主干应用程序中遇到一个未定义的错误。我不知道什么是未定义的。在路由器的索引函数中,我有

  var view = new Enki.Views.EntriesIndex();
  console.log(view);
  $('#container').html(view.render().el);
这最后一行给了我这个错误

未捕获的TypeError:无法读取未定义的属性“el”

console.log(view)
显示
view=new Enki.Views.EntriesIndex()。日志的一部分包含

   outerHTML: "<div><h1>Enki</h1>↵↵Entries go here↵</div>"
   outerText: "Enki↵↵Entries go here↵"
这是模板

  <h1>Enki</h1>

  <%= entries %>
Enki
…它没有插入到“container”div中,但正在视图中读取,如上面的日志输出所示。主页上的container div中没有插入任何内容

<div id="container">...</div>
。。。

谁能告诉我什么是“未定义”以及如何修复它

一个合理的假设是
render
函数不返回任何内容,因此
view.render()
是未定义的

尝试更改渲染函数,使其返回对
对象的引用

render: function(){
  $(this.el).html(this.template( {entries: "Entries go here"} ))
  return this // <- important
}
render:function(){
$(this.el).html(this.template({entries:“entries go here”}))

返回此//我猜@Michael还需要一些关于“未定义”的词。例如,“这是一个特殊值,用于定义无法获得的结果或未设置为任何特定值的变量。一些语言还使用
null
Empty
None
等来表示类似的含义。”好的,谢谢,这很有效,但是如果只是渲染没有返回任何内容,为什么我不能在第一行前面放一个返回,我尝试了,但没有成功。例如,return$(this.el)…另外,我想我读到你只需要返回这个;如果你想链接视图,这里不是这样。@Michael:
render
返回这个
,这是常规的,不是强制性的。这允许你在这么多主干代码中看到常见的
$x.append(v.render().el)
习惯用法。如果你返回
$(this.el)
使用
view.render().el
不再有意义,因为
render()
现在返回对DOM元素的jQuery选择。返回
this
将返回主干视图,该视图具有
el
属性。
render: function(){
  $(this.el).html(this.template( {entries: "Entries go here"} ))
  return this // <- important
}