【问题标题】:Javascript doesn't thnk 'class' object has been definedJavascript 不认为“类”对象已被定义
【发布时间】: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


【解决方案1】:

你已经在另一个函数中声明了NewsSlider,所以它的作用域是那个函数。

你试图在它不存在的函数之外使用它。

function NewsSlider(element, children) { ... } 移出匿名函数或将var a = new NewsSlider(); 移入其中。

由于您不使用 jQuery 或创建除 NewsSlider 之外的任何顶级范围变量,您不妨完全使用匿名函数。

【讨论】:

  • 就是这样。我真是个白痴。将来会派上用场,尽管我相信非常感谢您的回答:)
【解决方案2】:

这是一个作用域错误,是将代码放入闭包中的结果。这实际上是闭包的一个特性,也正是你应该使用它们的原因。所以你不能访问其中定义的变量。这是为了防止污染全局命名空间。

您可以在闭包之外定义 NewsSlider,您可以从闭包返回 NewsSlider 并将其分配给新的 var,或者您可以一起消除闭包。

选项 1:

var NewsSlider;

(function($) {
NewsSlider = function(element, children) {
    this.element = element;
    this.children = children;   
    this.currentItem  = 0;
    this.maxCount = this.children.length;
}
})(jQuery);

var a = new NewsSlider();

选项 2:

var NewsSlider = (function($) {
    var NewsSlider = function(element, children) {
        this.element = element;
        this.children = children;   
        this.currentItem  = 0;
        this.maxCount = this.children.length;
    }
    return NewsSlider;
})(jQuery);

var a = new NewsSlider();

选项 3:

function NewsSlider(element, children) {
    this.element = element;
    this.children = children;   
    this.currentItem  = 0;
    this.maxCount = this.children.length;
}

var a = new NewsSlider();

【讨论】:

    【解决方案3】:

    如果它不知道存在的对象,你不能指望它“认为”类已经定义。

    你已经在一个闭包中定义了你的类,因此一旦你离开了那个函数的范围,jQuery就不再有对它的引用,因此你会得到一个引用错误。

    考虑:

    --> function()
    |   {
    |       class aClass definition here
    |
    |       a = new aClass;
    |
    |       // do some work with the class here.
    |
    --> } 
    

    这里的一切都很好,因为类可以在当前范围内访问,但是考虑下面的第二个示例,我们尝试在包含类定义的函数范围之外为 p 分配一个值:

    --> function()
    |   {
    |       class aClass definition here
    |
    |       a = new aClass;
    |
    |       // do some work with the class here.
    |
    --> } 
    
    var p = a.getParam();
    

    p 试图从当前作用域中找不到的变量中获取其值,因此将在此处引发引用错误,因此会产生引用错误。

    为了解决这个问题,最好对你的类有一些全局引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      相关资源
      最近更新 更多