Calling function, from callback stored in object, in an array

Geoff

basically all I want to do is be able to call a function referenced in an object, for whatever reason i'm having major trouble, the error on chrome is : Uncaught TypeError: obj.draw is not a functionrender @ main.js:46main @ main.js:16 maybe this means it's private? I'm not sure, anyway's here's a MVCE

var bullets = [];
bullets.push(bullet)

;(function() // I know this is a little overkill for an mvce
{
  function main()
  {
    window.requestAnimationFrame( main );
    render();
  }
  main();
})();

function bullet()
{
  this.x = canvas.width/2;
  this.y = canvas.height/2;
  this.move = function()
  {
    ++this.y;
  };
  this.draw = function()
  {
    ctx.beginPath();
    ctx.rect(this.x, this.y, 5, 10);
    ctx.closePath();
    ctx.stroke();
  };
}


function render()
{
  for( let obj of bullets )
      obj.draw();
}
Praveen Kumar Purushothaman

You are pushing a class, but invoking an object's function. Here bullet is a class and not an instantiation of it. Only the instantiated variables will have the function. Change your code to:

bullets.push(new bullet());

And put the function declaration to the top. (Not necessary).

var bullets = [];
bullets.push(new bullet())

;(function() // I know this is a little overkill for an mvce
{
  function main()
  {
    window.requestAnimationFrame( main );
    render();
  }
  main();
})();

function bullet()
{
  this.x = canvas.width/2;
  this.y = canvas.height/2;
  this.move = function()
  {
    ++this.y;
  };
  this.draw = function()
  {
    ctx.beginPath();
    ctx.rect(this.x, this.y, 5, 10);
    ctx.closePath();
    ctx.stroke();
  };
}


function render()
{
  for( let obj of bullets )
      obj.draw();
}

The above code throws canvas isn't defined, which is expected for the snippet. Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP Calling a callback function from within Object method

From Dev

Angelscript calling overriding function from object in array

From Dev

Access correct "this" from callback function in array of functions inside an object

From Dev

Push object into local array from asynchronous callback function

From Dev

calling function stored as a property in an object in a for loop

From Dev

How to call function from object stored in array in PHP?

From Dev

Coffeescript: Calling array functions from a function in the same object

From Dev

calling a function from a stored procedure in oracle

From Dev

Call function from calling object

From Dev

Calling function with callback in nodejs

From Dev

Calling callback function in props

From Dev

Calling function with object parameter from another function

From Dev

React - Calling a function of all class instances in an array state stored in a ContextProvider

From Dev

Returning Array and assign to array from calling function

From Dev

Alternative to calling virtual/derived methods from constructor using callback function?

From

Calling a Go callback function from C++ through SWIG

From Dev

Node.js: Pass data from a callback to the calling function

From Dev

Calling a callback function from async.each and processing the results

From Dev

Calling a function in a callback function in NodeJs

From Dev

Reduce callback function to retrieve properties from an array

From Dev

Calling a Function stored in a class from a SubView is not updating the values on main ContentView

From Dev

Calling a method of a global object from a function

From Dev

TypeError: Object(...) is not a function, calling data from firebase

From Dev

Calling a function from a class without object

From Dev

Trying to create a object from a class by calling a function

From Dev

Calling javascript Service/Method from array object

From Dev

Knockout JS calling wrong object from array

From Dev

Calling a function from an object string compared to an object function

From Dev

NodeJs Javascript Calling a Function Dynamically From an Array

Related Related

  1. 1

    PHP Calling a callback function from within Object method

  2. 2

    Angelscript calling overriding function from object in array

  3. 3

    Access correct "this" from callback function in array of functions inside an object

  4. 4

    Push object into local array from asynchronous callback function

  5. 5

    calling function stored as a property in an object in a for loop

  6. 6

    How to call function from object stored in array in PHP?

  7. 7

    Coffeescript: Calling array functions from a function in the same object

  8. 8

    calling a function from a stored procedure in oracle

  9. 9

    Call function from calling object

  10. 10

    Calling function with callback in nodejs

  11. 11

    Calling callback function in props

  12. 12

    Calling function with object parameter from another function

  13. 13

    React - Calling a function of all class instances in an array state stored in a ContextProvider

  14. 14

    Returning Array and assign to array from calling function

  15. 15

    Alternative to calling virtual/derived methods from constructor using callback function?

  16. 16

    Calling a Go callback function from C++ through SWIG

  17. 17

    Node.js: Pass data from a callback to the calling function

  18. 18

    Calling a callback function from async.each and processing the results

  19. 19

    Calling a function in a callback function in NodeJs

  20. 20

    Reduce callback function to retrieve properties from an array

  21. 21

    Calling a Function stored in a class from a SubView is not updating the values on main ContentView

  22. 22

    Calling a method of a global object from a function

  23. 23

    TypeError: Object(...) is not a function, calling data from firebase

  24. 24

    Calling a function from a class without object

  25. 25

    Trying to create a object from a class by calling a function

  26. 26

    Calling javascript Service/Method from array object

  27. 27

    Knockout JS calling wrong object from array

  28. 28

    Calling a function from an object string compared to an object function

  29. 29

    NodeJs Javascript Calling a Function Dynamically From an Array

HotTag

Archive