Jump to content

JavaScript Factory Functions


Recommended Posts


I am new to this community and have a general question for folks here on the forms.  I am in process of learning ES5 Factory functions, which allow functions to return objects of data. Would anyone have any recommendations on an game engine that will allow me to continue coding in this style?



Link to comment
Share on other sites

There may be no complete game engines that allow you to do this, so my recommendation is you extend the third party libraries to your own game engine, like wrapping pixi.js (2D graphics) or Three.js (3D), and Howler.js (sound) into your own. They are good enough to start off quickly for you to make a game by wrapping them to your factory functions style with it.

The reason why I'm quite sure there is none is because JavaScript has many opinionated ways to make classes or objects derived from it (classic, prototypal, factory,... whatever I've grown tired of it, tbh). So of course, a complete game engine solution will use their own way too. One example, Cocos Creator uses John Resig's class inheritance as its base to make classes. Even Construct 2 has its own way that is barely even JavaScript when it comes to gameplay.

Link to comment
Share on other sites

Factory functions are just a pattern, one of many you'll need in your toolbox if you want to be a success.

Libraries/frameworks/etc might use that pattern, or they might use a different one, if you're using a framework I wouldn't recommend fighting against that frameworks idioms. The reason a framework works is that it dictates a certain style of coding and you get lots of advantages from working in that way.

Frameworks tend to want to 'own' the application and allow you windows in to that architecture to do your own stuff. Great for when you agree with that architecture (or don't know how to create your own) as it saves loads of boilerplate code and will solve a great number of problems so you don't have to.

Libraries/modules tend to be the opposite, you'll have to do the glueing together but they'll do the heavy lifting. Great for when you want more flexibility and are comfortable and confident enough to glue things together and deal with issues from doing so.

Link to comment
Share on other sites

  • 1 month later...

I recommend to read about factory functions here and here. I think that those examples and a detailed description of the code will give an understanding of how to act in different situations and the logic of actions will be generally understood. I don't often use this functionality, but for those rare assignments that I perform at work (or as a mentor at Assignment.EssayShark), you need to understand this function in general.

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

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.


  • Recently Browsing   0 members

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