I have a simple function that calls other functions:
function update() {
updateMissiles();
updatePlayer;
updateTurbines();
}
They are similar to each other in every way except updatePlayer will not run if I put brackets on the end of it. This doesn't break any code but I'm still curious why it does that?
4 Answers 4
I'm guessing there's an Exception in the updatePlayer method and since you're not calling it in the code you pasted above, you're not getting the Exception.
I would open up the Developer Tools for whatever browser you're using and see if there are any JavaScript Exceptions being thrown.
Comments
Without knowing more it's impossible to pinpoint exactly, but as a best guess - in the scope of the update function, the updatePlayer variable is not a function.
Try logging or debugging your javascript to find out what is going on.
Comments
A function will run only if you put () after it's name.
If you don't put brackets you'll get the function's content.
For example if you have:
function updatePlayer(){ alert('This is a player');}
And call it without brackets:
alert(updatePlayer);
the alerted output will be
function updatePlayer(){ alert('This is a player');}
This is used if you want to use callback functions.
Comments
You are confused. updatePlayer; doesn't invoke the updatePlayer function. updatePlayer(); does. Something else is going on in your code.
updatePlayer, it's impossible to say much. But a function won't run unless it's called, and calling a function means().updatePlayer? What do you meand by "updatePlayer will not run" and how you check if it has been run?updatePlayeris not a function by the timeupdateexecutes. Just beforeupdatePlayer, writeconsole.log(updatePlayer)to see what the value is.