Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8303115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:26:52+00:00 2026-06-08T17:26:52+00:00

I’m creating a game engine, or more like a large library of useful classes

  • 0

I’m creating a game engine, or more like a large library of useful classes and functions, for Javascript. I plan to use it both for some scientific simulations on the server side and on the client side, so the spectrum of functionality will be quite broad, but always it will revolve around a virtual world (a game, for example).

I’m not sure how to wrap it up, though. If I just provide all the classes, it would pollute the global namespace, which is quite bad. Can I just place everything inside one object, that acts as a namespace? Should the framework itself be a class that can be instanced?

If the later option is chosen, how do I handle classes inside classes (constructor functions)?

var Engine = function()
{
    this.someVar = 4;
}

Engine.prototype.Scene = function()
{
    this.entities = [];
    //What if the scene object needs some classes that are in the engine? How does it get it's parent engine object?
}

Engine.prototype.Scene.prototype.render = function()
{
    //"this" should now represent an instance of a scene. But how can I get someVar from the engine? How can I traverse up in the hierarchy of classes?
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:26:54+00:00Added an answer on June 8, 2026 at 5:26 pm

    I prefer to use what’s sometimes called a “revealing module” (… pattern). It looks like:

    var Engine = (function($)
    {
        $ = $ || {};
        var someVar = 4;
    
        $.Scene = function()
        {
            this.entities = [];
        }
    
        $.Scene.prototype.render = function()
        {
            // this function can access someVar just fine because of JavaScript's scoping rules
        }
    
        return $;
    })(Engine);
    

    This uses what’s called an immediately-invoked function expression (hereafter referred to as an IIFE) to form a closure within the Engine object. Due to JavaScript’s handling of scope, someVar is accessible to any function defined within the IIFE. The implication, however, is that no function can define it’s own someVar if it wants to refer to the someVar you define in the IIFE.

    The magic comes from the return statement. You can see that an object is returned, and within this object you must define anything you want to be “public”.

    The constructors, utility methods, etc. can then be accessed via Engine.Scene, which nicely namespaces your code.

    As for the $ argument, this is so that you can pass Engine to the function in each file, add some methods/properties/constructors (or create a new one if it doesn’t exist) and then pass the return value to another IIFE for further expansion.

    This is the method used in many popular JavaScript frameworks including jQuery, dojo and LimeJS


    scene.js:

    var Engine = (function ($) {
      // this creates a new object if Engine is undefined in the 
      // invocation below, and keeps the old object otherwise.
      // alternatively: if ($ === undefined) { $ = new Object; }
      $ = $ || {};
    
      $.foo = "foo";
      $.Scene = function () {
        // blah blah
      }
    
      // Engine holds either a newly created object,
      // or the old one if it was already defined
      return $;
    })(Engine);
    

    sprite.js:

    var Engine = (function ($) {
      $ = $ || {};
    
      $.Sprite = function () {
        // totally works
        this.bar = $.foo;
      }
    
      return $;
    })(Engine);
    

    You can then use them with something like:

    <script type="text/javascript" src="bar.js"></script>
    <script type="text/javascript" src="foo.js"></script>
    <script type="text/javascript">
      var mySprite = new Engine.Sprite;
      var myScene = new Engine.Scene;
    </script>
    

    You can substitute $ with whatever you like, $$ is common, or you can be clever. It’s just a placeholder for the global object you’re adding on to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.