fariazz

States as Objects or Functions

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.

Share this post


Link to post
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.

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.