0

Method of object fails, but property logs fine. So I declare a variable in the global scope, and try to assign an object to it within a function. The property 'id' traces correctly, but the method causes an error. I've looked for a similar post to this, but cannot find anything.

I'd rather program using OO in Javascript so it would be great if you could give me a pointer for this issue. Thanks in advance.

var currentEnemyPieceObject; // this gets set in the play function

function EnemyPieceObject( _id ){

  this.id = _id;
  this.pathArray = [];
  this.active = false;

}

EnemyPieceObject.prototype = {

  constructor:EnemyPieceObject,
  addPointToPathArray:function( xPos, yPos ){ 
    var point = { "x":xPos, "y":yPos };
    this.pathArray.push( point );
  }
}


function play() {

  currentEnemyPieceObject  =  new EnemyPieceObject( 0 );

  console.log( currentEnemyPieceObject.id ); // result is 0

  currentEnemyPieceObject.addPointToPathArray( 0, 0 );
  // results in error
  // Uncaught TypeError: Uncaught TypeError:
  // currentEnemyPieceObject.addPointToPathArray is not a function

}
4
  • 1
    "this.currentEnemyPieceObject..." from the error message does not match the actual code you're showing here...!? Commented Mar 9, 2016 at 11:28
  • thanks deceze - yes - I was just trying out some other options. Thanks - it still fails. I've pasted in the correct error. ( eagle eyes :) ) Commented Mar 9, 2016 at 11:31
  • Well... that code as is doesn't throw any such error... Commented Mar 9, 2016 at 11:35
  • 2
    This code woks ok... what's the problem? What browser (and version) are you using? I actually copy pasted... did not modified any single char - and is ok. Commented Mar 9, 2016 at 11:37

1 Answer 1

1

The problem might be that you are calling the play() function before the object is initialised. Run the code snippet below with the console window open (usually F12). The error you reported happens when play() is called too early. However, it works as expected when called later.

var currentEnemyPieceObject; 

try {
  play();
}
catch(e) { console.error( e.message ); } 
// prints "currentEnemyPieceObject.addPointToPathArray is not a function"

function EnemyPieceObject( _id ){
  this.id = _id;
  this.pathArray = [];
  this.active = false;
}
EnemyPieceObject.prototype = {
  constructor:EnemyPieceObject,
  addPointToPathArray:function( xPos, yPos ){ 
    var point = { "x":xPos, "y":yPos };
    this.pathArray.push( point );
  }
}
function play() {
  currentEnemyPieceObject  =  new EnemyPieceObject( 0 );
  currentEnemyPieceObject.addPointToPathArray( 0, 0 );
}

play(); // no errors

console.info( typeof currentEnemyPieceObject.addPointToPathArray );  // prints "function"

Sign up to request clarification or add additional context in comments.

1 Comment

Roberto, Thanks, I've learned something, well answered - thanks for your time :) So the moral of the story is to define all of your Classes literally before executing any code in Javascript. ( My AS3 background did not lead me to this conclusion )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.