Uncaught Type Error: Undefined is not a function JavaScript Module

Mdd

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

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.

I am not sure what is causing this error. Here's my HTML and JavaScript along with a fiddle.

Fiddle.

HTML:

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

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. So please let me know if I have anything else that is incorrect. Thanks!

rvighne

You are defining MyClass after you run the code that uses 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. definitions out of the actual MyClass. Put them after.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Uncaught Type Error: Undefined is not a function JavaScript Module

From Dev

javascript uncaught type error function ajax

From Dev

uncaught type error:$ is not a function

From Dev

Trying to make a clock in JavaScript, getting an error, Uncaught TypeError: undefined is not a function

From Dev

Uncaught Error: Call to undefined function

From Dev

Uncaught TypeError: undefined is not a function - Javascript

From Dev

uncaught typeerror undefined is not a function in javascript

From Dev

Uncaught Reference Error in Javascript & undefined

From Dev

Uncaught Reference Error in Javascript & undefined

From Dev

Uncaught type error $(...)textillate is not a function

From Dev

Uncaught type error: $(...).datepicker() is not a function

From Dev

Uncaught Type Error: Cannot set property of undefined

From Dev

drupal jquery error: Uncaught TypeError: undefined is not a function

From Dev

drupal jquery error: Uncaught TypeError: undefined is not a function

From Dev

"Uncaught TypeError: undefined is not a function" console error appearing

From Dev

Why is there an error, "Uncaught TypeError: undefined is not a function"?

From Dev

Error message "Uncaught TypeError: undefined is not a function" (DataTable)

From Dev

Javascript undefined function inside module

From Dev

Javascript regex .test() "Uncaught TypeError: undefined is not a function"

From Dev

Jquery / Javascript - Uncaught TypeError: undefined is not a function

From Dev

"Uncaught TypeError: undefined is not a function" in JavaScript code block

From Dev

Jquery / Javascript - Uncaught TypeError: undefined is not a function

From Dev

undefined is not a function - error in Javascript

From Dev

Undefined is not a function javascript error

From Dev

Javascript Error undefined is not a function

From Dev

Javascript Module Pattern Uncaught Reference Error

From Dev

"Uncaught TypeError: undefined is not a function" Error message in Javascript when calling a method in an object

From Dev

Getting a error "Uncaught TypeError: undefined is not a function" when calling a function

From Dev

Uncaught Type Error in javascript: object has no method

Related Related

  1. 1

    Uncaught Type Error: Undefined is not a function JavaScript Module

  2. 2

    javascript uncaught type error function ajax

  3. 3

    uncaught type error:$ is not a function

  4. 4

    Trying to make a clock in JavaScript, getting an error, Uncaught TypeError: undefined is not a function

  5. 5

    Uncaught Error: Call to undefined function

  6. 6

    Uncaught TypeError: undefined is not a function - Javascript

  7. 7

    uncaught typeerror undefined is not a function in javascript

  8. 8

    Uncaught Reference Error in Javascript & undefined

  9. 9

    Uncaught Reference Error in Javascript & undefined

  10. 10

    Uncaught type error $(...)textillate is not a function

  11. 11

    Uncaught type error: $(...).datepicker() is not a function

  12. 12

    Uncaught Type Error: Cannot set property of undefined

  13. 13

    drupal jquery error: Uncaught TypeError: undefined is not a function

  14. 14

    drupal jquery error: Uncaught TypeError: undefined is not a function

  15. 15

    "Uncaught TypeError: undefined is not a function" console error appearing

  16. 16

    Why is there an error, "Uncaught TypeError: undefined is not a function"?

  17. 17

    Error message "Uncaught TypeError: undefined is not a function" (DataTable)

  18. 18

    Javascript undefined function inside module

  19. 19

    Javascript regex .test() "Uncaught TypeError: undefined is not a function"

  20. 20

    Jquery / Javascript - Uncaught TypeError: undefined is not a function

  21. 21

    "Uncaught TypeError: undefined is not a function" in JavaScript code block

  22. 22

    Jquery / Javascript - Uncaught TypeError: undefined is not a function

  23. 23

    undefined is not a function - error in Javascript

  24. 24

    Undefined is not a function javascript error

  25. 25

    Javascript Error undefined is not a function

  26. 26

    Javascript Module Pattern Uncaught Reference Error

  27. 27

    "Uncaught TypeError: undefined is not a function" Error message in Javascript when calling a method in an object

  28. 28

    Getting a error "Uncaught TypeError: undefined is not a function" when calling a function

  29. 29

    Uncaught Type Error in javascript: object has no method

HotTag

Archive