Last active
April 13, 2017 01:20
-
-
Save auxiliary/46c32c90c037d71d2bec471f66bd126d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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