What is the 'this' keyword refer to in 'this.mod
What is the 'this' keyword refer to in 'this.model.toJSON'?
有人能向我解释一下"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对象。
相关文章:
- $(this).prop('property') vs. this.property
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- Twitter Bootstrap typeahead:使用“this”获取上下文/调用元素
- 在另一个函数中使用变量this
- this.router在AngularJS 2中未定义
- reactjs this.refs vs document.getElementById
- JavaScript 中的嵌套函数和 “this” 关键字
- jQuery AJAX write to XML
- React+Meteor:this.ops返回未定义
- grep in JQuery to C#
- how to convert html <div> to pdf
- pass 'this' to alertify
- 可以't find why this js/jQuery to list last 12 months gene
- Javascript bind "this" to event
- Turn '$(this)' jquery to javascript 'this'
- 通过“this.value"to addEventListener功能
- jQuery blur() not listening to $(this)
- What is the 'this' keyword refer to in 'this.mod
- JavaScript: change function(this) to function(this.id)
- ES6类:access to 'this'& # 39; addeventlistener # 39;应