Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const Application = ((d) => {
const privateVariable = 'Private content'
const __private = {
cache: () => {
this.link = d.querySelector('.link')
},
bind: () => {
this.link.addEventListener('click', this.showContent, false)
},
showContent: () => console.log(privateVariable)
}
return {
init: () => {
__private.showContent()
__private.cache()
__private.bind()
}
}
})(document)
Application.init()
console.log(privateVariable) //undefined
var Application = (function(w, d){
var myPrivateVariable = "My private content";
var __private = {
cache: function(){
this.link = d.querySelector('.link-item');
}
bind: function(){
this.link.addEventListener('click', this.handleClick, false);
},
handleClick: function(){
console.log(myPrivateVariable);
}
};
return {
init: function(){
console.log(myPrivateVariable);
__private.cache();
__private.bind();
}
};
})(window, document);
Application.init(); //init the app
console.log(myPrivateVariable); // undefined
@3emad

This comment has been minimized.

Copy link

@3emad 3emad commented Jan 15, 2013

Thanks for sharing this, its awesome!

@albiere

This comment has been minimized.

Copy link

@albiere albiere commented Mar 8, 2014

I really like this approach!
However, I'd rather improve the public API in this case. Why are the functions "cache" and "bind" public?

What do you think?

@guilhermepontes

This comment has been minimized.

Copy link
Owner Author

@guilhermepontes guilhermepontes commented Jan 12, 2015

@albiere makes sense. Updated! :)

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