gpt4 book ai didi

javascript - 窗口的主干范围而不是这个

转载 作者:行者123 更新时间:2023-11-30 18:09:36 25 4
gpt4 key购买 nike

为什么这个日志记录为窗口而不是 Backbone 对象?

App.Models.Site = Backbone.Model.extend({
url: 'assets/json/app.json',

initialize: function(){
this.fetch({success:this.success});
},

success: function(){
console.log('success', this.attributes); // log's: success undefined
console.log(this); // window
}
});

最佳答案

因为该函数由 jQuery(或您使用的任何 DOM 库)ajax 函数调用。

使用 this.fetch({success:_.bind(this.success, this)});

关于javascript - 窗口的主干范围而不是这个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14936869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com