Skip to content

Instantly share code, notes, and snippets.

@rezoner
Last active August 29, 2015 14:20
Show Gist options
  • Save rezoner/de8a10ab78f52e4c2f64 to your computer and use it in GitHub Desktop.
Save rezoner/de8a10ab78f52e4c2f64 to your computer and use it in GitHub Desktop.
Imaginary namespaces for javascript

Namespacing concept for javascript

keyword namespace would replace window as a default global with another object until namespace is escaped.

namespace ENGINE;

Physics = {

  applyForce: function(x, y) { ... },
  processEntity: function(entity) { ... }

};

World = function() { ... };

namespace; /* escape namespace */

Global variables resolve to ENGINE.Physics, ENGINE.World

Lookup chain

If a variable is undefined for current namespace compiler looks for variable in a main global (window)

namespace ENGINE;
 
Math.cos(0.25); /* it's still window.Math.cos */

namespace;

Access variable from root scope:

namespace ENGINE;

Math = { }; /* overwrite */

window.Math /* still accessible as usual */

namespace;
@rezoner
Copy link
Author

rezoner commented May 6, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment