简体   繁体   English

未捕获类型错误:未定义不是函数JavaScript模块

[英]Uncaught Type Error: Undefined is not a function JavaScript Module

I am trying to pass a reference to a jQuery Object to a module and have been getting an error that says: Uncaught TypeError: undefined is not a function 我试图将一个jQuery对象的引用传递给一个模块,并且一直收到一条错误消息: Uncaught TypeError:undefined不是函数

I am working on a fiddle to try and understand this pattern that I saw, as well as to understand prototype and the new keyword. 我工作的一个fiddle ,试图理解这个模式,我看到,以及了解原型和新的关键字。

I am not sure what is causing this error. 我不确定是什么导致了这个错误。 Here's my HTML and JavaScript along with a fiddle . 这是我的HTMLJavaScript以及fiddle

Fiddle. 小提琴。

HTML : HTML

<div class="js-container" data-options="true"></div>
<div class="js-container" data-options="false"></div>

JavaScript : JavaScript

$('.js-container').each(function (i, element) {
    new MyClass($(element));
    console.log($(element));
});
var MyClass = (function ($element) {
    this.$element;
    console.log(this.$element);    
    this.init();    
    MyClass.prototype.init = function () {
        this.addText();        
        return this;
    };    
    MyClass.prototype.addText = function () {
        var optText = this.$element.attr('data-options');
        this.$element.text(optText);        
        return this;
    };    
}());

I am trying to learn to write more module with JavaScript . 我正在努力学习用JavaScript编写更多模块。 So please let me know if I have anything else that is incorrect. 所以,如果我有其他不正确之处,请告诉我。 Thanks! 谢谢!

You are defining MyClass after you run the code that uses MyClass. 在运行使用MyClass的代码后,您正在定义MyClass。 Also, stuffing your class into a variable rather than declaring it the "traditional" way causes some problems. 此外,将您的类填充到变量而不是将其称为“传统”方式会导致一些问题。 Try this: 试试这个:

function MyClass ($element) {

  this.$element;
  console.log(this.$element);

  // more code...
}

$('.js-container').each(function (i, element) {
  new MyClass($(element));
  console.log($(element));
});

Also, move your MyClass.prototype. 另外,移动MyClass.prototype. definitions out of the actual MyClass. 实际MyClass中的定义。 Put them after. 把它们放在后面。

For what it is worth, a lot of the time when I see this problem I find that I have inadvertently called a function before it was referenced in the html. 对于它的价值,很多时候当我看到这个问题时,我发现我在html中引用它之前无意中调用了一个函数。 For example, I just called a jquery method in a js file before I loaded my jquery files. 例如,我在加载jquery文件之前在js文件中调用了jquery方法。 Putting the jquery references further up on the page solved it. 将jquery引用进一步放在页面上解决了它。

None of the above answers worked for me. 上述答案都不适合我。 But of course, my problem was special. 但当然,我的问题很特别。 I was recycling an old project to make a new one and there was a method that made the class that I had forgotten about (and was passing in a string instead of a function), and the fact that it wasn't updated was giving me this error, even though I had the syntax elsewhere all correct. 我正在回收一个旧项目来创建一个新项目,并且有一个方法使我忘记了这个类(并且传入了一个字符串而不是一个函数),并且它没有更新的事实给了我这个错误,即使我在其他地方的语法都正确。

Moral: if you try the above solutions and it still doesn't work, desk check and make sure everyplace where the surrounding method is called passes in the right parameters. 道德:如果您尝试上述解决方案并且仍然无效,请检查并确保调用周围方法的每个位置都传递正确的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM