Jump to content

States as Objects or Functions


fariazz
 Share

Recommended Posts

When creating States I've seen both people using Objects and Functions.

 

I've noticed that in the Phaser Coding Tips all the examples are using Functions.

 

Is there any advantage in using Functions over Objects in this case?

 

I understand that using Functions + prototype is good for when you are creating classes and then instances of those classes, but in this case there is only 1 of each State in a game.

Link to comment
Share on other sites

Both work, so it is mostly a matter of personal preference.

 

I've used functions for states in the past because I've used the new operator and tried to keep a sort of OOP/classes style to my code. As everything else has used function and prototype, I didn't want to code states in a different way. Also, Phaser itself uses function/prototype for classes so following that style makes sense if your game relies on Phaser as a framework.

 

Objects are a little bit lighter weight but if you are just creating one instance of them that won't have much of an effect on performance.

 

I am, personally, trying to move all my code into a more object-based model - i.e. not using the new operator but Object.create instead.

 

It is JavaScript so there are lots of ways of doing everything, most of which have good and bad aspects.

Link to comment
Share on other sites

Both work, so it is mostly a matter of personal preference.

 

I've used functions for states in the past because I've used the new operator and tried to keep a sort of OOP/classes style to my code. As everything else has used function and prototype, I didn't want to code states in a different way. Also, Phaser itself uses function/prototype for classes so following that style makes sense if your game relies on Phaser as a framework.

 

Objects are a little bit lighter weight but if you are just creating one instance of them that won't have much of an effect on performance.

 

I am, personally, trying to move all my code into a more object-based model - i.e. not using the new operator but Object.create instead.

 

It is JavaScript so there are lots of ways of doing everything, most of which have good and bad aspects.

 

That makes sense thanks for the explanation!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...