【发布时间】:2015-03-13 05:50:04
【问题描述】:
这已经困扰了我好几个小时了。我有一个文件,其中定义了一个 Javascript 类对象,然后在定义之后立即实例化 clas。我收到一个未定义的错误,就好像该类没有定义一样:
(function($) {
function NewsSlider(element, children) {
this.element = element;
this.children = children;
this.currentItem = 0;
this.maxCount = this.children.length;
}
NewsSlider.prototype.displayItem = function() {
this.children.hide();
this.children[this.currentItem].show();
}
NewsSlider.prototype.startNewSlider = function() {
if(this.currentItem > this.maxCount) {
this.currentItem = 0;
}
setTimeout(function() {
this.displayItem();
}, 5000);
}
})(jQuery);
var a = new NewsSlider();
这是我得到的错误:
ReferenceError: NewsSlider is not defined
var newsSliders = new NewsSlider("#news-ticker ul", "#news-ticker ul li");
现在,我似乎在这段代码中没有看到任何问题,并且之前已经这样做了很多次,所以有人可以指出我出了什么问题或者我在哪里愚蠢吗? 谢谢
【问题讨论】:
-
在你所指的范围内没有定义。
-
你在一个函数中定义它。它不在范围内
-
超出了您调用它的范围。
标签: javascript jquery class object