Skip to content

Instantly share code, notes, and snippets.

@ui2code
Created November 27, 2014 05:43
Show Gist options
  • Save ui2code/2fcafbc365240e16a969 to your computer and use it in GitHub Desktop.
Save ui2code/2fcafbc365240e16a969 to your computer and use it in GitHub Desktop.
My Module
// https://github.com/hemersonvianna/javascript-patterns
// https://github.com/fhferreira/aprendendo-padroes-de-projeto-javascript
//Início de APP.ModuloComplexo
var APP = APP || {};
APP.ModuloPai = {
_nome: "Nestor",
//Quando iniciar o módulo
setUp: function() {
console.debug("APP.ModuloPai iniciado");
console.debug(">>> Acessando _nome do ModuloFilho", this.ModuloFilho._nome);
},
ModuloFilho: {
_nome: "Dennis",
//Quando iniciar o módulo filho
setUp: function() {
console.debug("APP.ModuloComplexo.ModuloFilho iniciado");
console.debug(">>> Acessando _nome de ModuloPai", this.pai()._nome);
}
}
};
//FIM de APP.ModuloComplexo
console.log( APP.ModuloPai );
var MyModule = (function() {
var myPrivateData = 303;
function myPrivateFunction() {
alert('private');
}
return {
myPublicData : 42,
myPublicFunction : function() {
alert('public');
}
};
})();
console.log( MyModule );
var namespace = function(path, context, args) {
var finalLink = namespace._generateChain(path, window);
context.apply(finalLink, [finalLink].concat(args));
};
namespace._generateChain = function(path, root) {
var segments = path.split('.'),
cursor = root,
segment;
for (var i = 0; i < segments.length; ++i) {
segment = segments[i];
cursor = cursor[segment] = cursor[segment] || {};
}
return cursor;
};
namespace("MAG", function (self) {
self.doSomethingElse = function () {
console.log("something else");
};
});
console.log( MAG.doSomethingElse() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment