Skip to content

Instantly share code, notes, and snippets.

@dutradotdev
Last active March 11, 2019 02:17
Show Gist options
  • Save dutradotdev/2127702893226474209b9d10a0b8b17a to your computer and use it in GitHub Desktop.
Save dutradotdev/2127702893226474209b9d10a0b8b17a to your computer and use it in GitHub Desktop.
Namespace usando IIFE
(function(namespace){
'use strict';
function getName() {
return 'Lucas';
}
namespace.greeting = {
hi: function hi() {
console.log('hi ' + getName());
},
hello: function hello() {
console.log('hello ' + getName());
}
};
}(this.cokePkt || (this.cokePkt = {})));
// Tente executar a função hi() e hello() que colocamos dentro
// do objeto greeting que está dentro do nosso namespace:
cokePkt.greeting.hi(); //hi Lucas
cokePkt.greeting.hello(); //hello Lucas
// tente executar a funcão getName():
cokePkt.greeting.getName(); //Uncaught TypeError: cokePkt.greeting.getName is not a function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment