Ask coding questions

← Back to all posts
My function becomes randomly undefined!
ShivankChhaya (241)

Hi.

In my templates/battle.html file, If you search for the enemy movement comment, I have two console.logs. They say the type of movers.setit is a function, and the exact line after that says it is not a function. Could someone give me some insight on this?

Answered by Coder100 (18903) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (18903)

psst are you trying to use something like classes?

ShivankChhaya (241)

@Coder100 ummmmmmmm
A. is that related to my question?
B. whaddya mean?

Coder100 (18903)

yes of course because you are creating an object, and you are using this. Also you are making things like methods, and the function you are console.logging literally is a 'method'. @ShivankChhaya

ShivankChhaya (241)

@Coder100 sooooooooooo how would i call it?

Coder100 (18903)

well
I recommend using this syntax instead:

function Movers() {
  this.tomove = ...;
  this.... = ...;
}

Movers.prototype.setit = ...

@ShivankChhaya

ShivankChhaya (241)

@Coder100 but where exactly? (Sorry if i'm being annoying)

Coder100 (18903)

replace the Movers object you defined with that syntax instead @ShivankChhaya

ShivankChhaya (241)

@Coder100 Thanks! In the final post, you'll be mentioned and be given credit with helping the game.