Skip to content

Instantly share code, notes, and snippets.

@niharsawant
Created September 16, 2012 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niharsawant/3731773 to your computer and use it in GitHub Desktop.
Save niharsawant/3731773 to your computer and use it in GitHub Desktop.
Encapsulation in Javascript and Coffeescript
class person
constructor : (@name) ->
doPrivateThings = () ->
alert('Psst ' + @name + ' is doing private things.')
#using '=' in function declaration will make it private
goToRestroom : ->
alert(@name + ' is inside restroom.')
doPrivateThings.call(this)
jim = new person('Jim')
jim.goToRestroom()
var jim, person;
person = (function() {
var doPrivateThings;
function person(name) {
this.name = name;
}
doPrivateThings = function() {
return alert('Psst ' + this.name + ' is doing private things.');
};
person.prototype.goToRestroom = function() {
alert(this.name + ' is inside restroom.');
return doPrivateThings.call(this);
};
return person;
})();
jim = new person('Jim');
jim.goToRestroom();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment