What is the 'this' keyword refer to in 'this.mod

What is the 'this' keyword refer to in 'this.model.toJSON'?

本文关键字:this to in mod refer keyword is What the      更新时间:2023-09-26

有人能向我解释一下"this.model. tojson()"中的'this'指的是什么吗?我的推理是,"this"应该指的是对象ContactView,因为这是它所在的对象。但事实似乎并非如此。"这。模型’连接到变量Contact?

var Contact = Backbone.Model.extend({
    defaults: {
    photo: "/img/placeholder.png"
  }
});
var ContactView = Backbone.View.extend({
   tagName: "article",
   className: "contact-container",
   template: $("#contactTemplate").html(),
   render: function () {
      var tmpl = _.template(this.template);
    this.$el.html(tmpl(this.model.toJSON()));
    return this;
   }
});
 contact = new Contact({name:Mike,location:'Chicago'});
 contactView = new ContactView({model: contact});
 contactView.render();

是的,你是对的。'this'有当前上下文,在你的例子中它是当前视图对象。如果您正在使用上面的代码片段执行视图,this.model.toJSON()将返回json对象。