Javascript 这是未定义的

Javascript 这是未定义的,javascript,backbone.js,Javascript,Backbone.js,我的脊梁骨一直有问题,向几个人寻求帮助,但到目前为止运气不佳 我创建了一个像这样的新集合 let eventsCollection = new BandEvents({artist: 'Muse'}); 在收藏中,我这样做 const BandEvents = Collection.extend({ model: Artist, url: 'http://api.bandsintown.com/artists/' + this.artist + '/events.json?ap

我的脊梁骨一直有问题,向几个人寻求帮助,但到目前为止运气不佳

我创建了一个像这样的新集合

let eventsCollection = new BandEvents({artist: 'Muse'});
在收藏中,我这样做

const BandEvents = Collection.extend({
    model: Artist,
    url: 'http://api.bandsintown.com/artists/' + this.artist + '/events.json?api_version=2.0&app_id=SomeID',

});
运行此代码时,在浏览器控制台中出现以下错误

Uncaught TypeError: Cannot read property 'artist' of undefined

因此,
+this.artist+
中的this未定义。我不知道我做错了什么。

这是未定义的,因为在此处设置url时集合未实例化。如果需要动态设置,我建议使用url函数:

这是未定义的,因为在此处设置url时集合未实例化。如果需要动态设置,我建议使用url函数:

我就是这样解决的

我变了

let eventsCollection = new BandEvents({artist: 'Muse'});

(我需要从输入字段中获取艺术家名称)

改变

const BandEvents = Collection.extend({
    model: Artist,
    url: 'http://api.bandsintown.com/artists/' + this.artist + '/events.json?api_version=2.0&app_id=SomeID',

});


为此,我使用ES6的

这就是我解决问题的方法

我变了

let eventsCollection = new BandEvents({artist: 'Muse'});

(我需要从输入字段中获取艺术家名称)

改变

const BandEvents = Collection.extend({
    model: Artist,
    url: 'http://api.bandsintown.com/artists/' + this.artist + '/events.json?api_version=2.0&app_id=SomeID',

});

为此,我使用ES6