Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save auxiliary/46c32c90c037d71d2bec471f66bd126d to your computer and use it in GitHub Desktop.
Save auxiliary/46c32c90c037d71d2bec471f66bd126d to your computer and use it in GitHub Desktop.
// An OLN namespace
namespace = {
foo: function(){ alert('namespace.foo'); },
bar: function(){ alert('namespace.bar'); },
};
// A namespace
(function(projecto){
projecto.flag = true;
projecto.foo = function()
{
alert('projecto.foo');
}
}(window.projecto = window.projecto || {}));
// Note: !function... would also work. It just needs to be an expression.
// So does (function(){})(...); and ;!function...
// Extending the previous namespace
(function(projecto){
projecto.bar = function()
{
alert('projecto.bar');
}
}(window.projecto = window.projecto || {}));
// A simplified version of the previous namespace
window.space = window.space || {};
space.foo = function(){ alert('space.foo'); };
// an object/class and its constructor lol
function test()
{
alert('constructor');
this.foo = function(){ alert('foo'); };
}
// adding a method to the class (prototype)
// test.bar = function(){ alert('bar'); }; would not add bar to "t"
test.prototype.bar = function(){ alert('bar'); };
// Tests
projecto.bar();
namespace.foo();
space.foo();
t = new test();
t.foo();
t.bar();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment