【问题标题】:what does "this" refer to?“这个”指的是什么?
【发布时间】:2017-05-04 22:18:42
【问题描述】:

thisinitializerender 属性中指的是什么?

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 代码:

<!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>

【问题讨论】:

标签: javascript backbone.js


【解决方案1】:

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

尝试以下方法:

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

看看这些结果是如何相同/不同的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-16
    • 2013-01-16
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 2021-12-28
    相关资源
    最近更新 更多