Why is a function wrap unside a function object not called in javascript

Snedden27

I have JavaScript function as object:

function hexMesh(){
     var side=25;
     console.log('hexMesh');

     function drawOver(){
     }
}  

As you can see it has a function call drawOver.

I try to call it by using a constructor as follows:

window.onload = function() {
    hexMeshObj=new hexMesh();
    hexMeshObj.drawOver();
}

But it gives me error saying undefined is not a function

Now I know I could declare the function in the prototype of the object but I don't want to do that.

Here it is on JSFiddle.

Schahriar SaffarShargh

You can't use JavaScript like that!

Solution: Use Class-like prototypes (please don't treat them as classes, although they provide inheritance)

var x = function(v){ this.test = v; } // This is your constructor
x.prototype.show = function(){ console.log("TEST", this.test); } // This is a prototype with this as a context

var y = new x(true);
y.show(); // Logs TEST true

Edit: Alternatively (although the prototype way is better as it provides real inheritance the oop way)

var x = function(v){
var context = this;
this.test = v;
this.show = function(){
    console.log('test', context.test)
});

Another alternative way is to use bind to bind context if you need it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is a function wrap unside a function object not called in javascript

From Dev

Why is the function called? JavaScript / Window

From Dev

How to create a function that can be called on an object in javascript

From Dev

Why is my JavaScript function apparently not being called?

From Dev

wrap function in javascript by curry

From Dev

Called object 'time' is not a function

From Dev

Called object 'time' is not a function

From Dev

Why the function unexpected is not called?

From Dev

Why ordinary function is not called there?

From Dev

Why is this function being called?

From Dev

Why is this function not being called?

From Dev

Is there a way to wrap a jquery function to an object?

From Dev

external function not called - javascript

From Dev

JavaScript called function and scope

From Dev

JavaScript function called "action"

From Dev

external function not called - javascript

From Dev

Javascript function called by itself

From Dev

Javascript Function not called onclick

From Dev

JavaScript function called "action"

From Dev

Javascript Function to be called in PHP

From Dev

Why am I getting a 'called object 0 is not a function error'?

From Dev

Why my function creates a new object every time it is called?

From Dev

Why am I getting a 'called object 0 is not a function error'?

From Dev

Why is there two Object for a function in the Javascript prototype chain

From Dev

Wrap in promise JavaScript generic function

From Dev

Why the destructor was called here and how an object member function could be called after calling that object destructor?

From Java

Javascript Check in the function, if it being called directly or from an object

From Dev

Why is this Javascript prototype function called and who calls it with undefined?

From Dev

Why is my javascript function not being called in my div?

Related Related

  1. 1

    Why is a function wrap unside a function object not called in javascript

  2. 2

    Why is the function called? JavaScript / Window

  3. 3

    How to create a function that can be called on an object in javascript

  4. 4

    Why is my JavaScript function apparently not being called?

  5. 5

    wrap function in javascript by curry

  6. 6

    Called object 'time' is not a function

  7. 7

    Called object 'time' is not a function

  8. 8

    Why the function unexpected is not called?

  9. 9

    Why ordinary function is not called there?

  10. 10

    Why is this function being called?

  11. 11

    Why is this function not being called?

  12. 12

    Is there a way to wrap a jquery function to an object?

  13. 13

    external function not called - javascript

  14. 14

    JavaScript called function and scope

  15. 15

    JavaScript function called "action"

  16. 16

    external function not called - javascript

  17. 17

    Javascript function called by itself

  18. 18

    Javascript Function not called onclick

  19. 19

    JavaScript function called "action"

  20. 20

    Javascript Function to be called in PHP

  21. 21

    Why am I getting a 'called object 0 is not a function error'?

  22. 22

    Why my function creates a new object every time it is called?

  23. 23

    Why am I getting a 'called object 0 is not a function error'?

  24. 24

    Why is there two Object for a function in the Javascript prototype chain

  25. 25

    Wrap in promise JavaScript generic function

  26. 26

    Why the destructor was called here and how an object member function could be called after calling that object destructor?

  27. 27

    Javascript Check in the function, if it being called directly or from an object

  28. 28

    Why is this Javascript prototype function called and who calls it with undefined?

  29. 29

    Why is my javascript function not being called in my div?

HotTag

Archive