Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
const Application = (() => {
const privateVariable = 'Private content'
const link = document.querySelector('.link')
const _private = {
cache: () => this.link = document.querySelector('.link'),
bind: () => this.link.addEventListener('click', this.showContent, false),
showContent: () => console.log(privateVariable)
}
return {
init: () => {
_private.showContent()
_private.cache()
_private.bind()
}
}
})()
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.

Show comment Hide comment
@3emad

3emad Jan 15, 2013

Thanks for sharing this, its awesome!

3emad commented Jan 15, 2013

Thanks for sharing this, its awesome!

@albiere

This comment has been minimized.

Show comment Hide comment
@albiere

albiere 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?

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.

Show comment Hide comment
@guilhermepontes

guilhermepontes Jan 12, 2015

@albiere makes sense. Updated! :)

Owner

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