“这个”指的是什么?

时间:2016-12-19 19:37:19

标签: javascript backbone.js

thisinitialize属性中render引用的内容是什么?

JavaScript代码:

SearchView = Backbone.View.extend({ 
  initialize: function(){
    this.render(); 
  },
  render: function(){
    // Compile the template using underscore
    var template = _.template( $("#search_template").html(), {} );
    // Load the compiled HTML into the Backbone "el"
    this.$el.html( template ); 
  }
});
var search_view = new SearchView({ el: $("#search_container") });

HTML code:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Backbone.js App</title>
  <meta name="description" content="">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
</head>
<body>
<div id="search_container"></div>

<script type="text/template" id="search_template">
  <label>Search</label>
  <input type="text" id="search_input" />
  <input type="button" id="search_button" value="Search" />
</script>

<script src="app.js"></script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

在您的具体情况下,它是search_view

尝试以下方法:

  • 在初始化方法中添加console.log('this in initialize', this)
  • 在您的渲染方法中添加console.log('this in render', this)
  • console.log('search_view', search_view)
  • 之后添加var search_view = new SearchView(...);

并了解这些结果是如何相同/不同的。