当我实例化一个对象时,它应该使用 jQuery 的 getJSON 方法获取数据并从该数据中填充对象的属性。相反,我开始Uncaught Reference: data is not defined
了 AJAX 调用。data
,在这种情况下,是我传递给回调函数的 JSON 数据对象参数的名称。
我的构造函数:
function HeadlineList(url) {
this.url = url;
this.checkEmpty = function() {
if (this.headlines === 0) {
this.refreshContent();
}
};
this.getRandom = function(remove) {
var headlineNumber = Math.floor(Math.random()*this.quantity);
var headline = this.headlines[headlineNumber];
if (remove) {
this.deleteHeadline(headlineNumber);
}
return headline;
};
this.getHeadline = function(number, remove) {
var headline = this.headlines[number]
if (remove) {
this.deleteHeadline(number);
}
return headline;
};
this.deleteHeadline = function(number) {
this.headlines.splice(number, 1);
this. quantity -= 1;
};
this.fillFromJSON = function(data) {
this.headlines = data.headlines;
this.quantity = this.list.length;
};
this.refreshContent = function() {
$.getJSON(this.url, this.fillFromJSON(data));
};
this.refreshContent();
}
我正在像这样实例化一个对象:
headlines = new HeadlineList('js/headlines.json');
谁能帮我弄清楚为什么这不起作用?