Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hyper Private Variables in Javascript

Hyper Private Variables in Javascript

Or, a merry romp through the fields of "why are javascript objects kinda weird"?

Avatar for Ben Green

Ben Green

May 16, 2013
Tweet

More Decks by Ben Green

Other Decks in Technology

Transcript

  1. prototype inheritance: Message.prototype.greet = function (who) { return this.greeting +

    " " + who; } method invocation: var greeting = helloMessage.greet("world"); >> “hello world” function Message(param) { this.greeting = param; } var sayHello = new Message('hello');
  2. function Counter() { var increment, decrement, get; (function() { var

    privateCount = 0; increment = function() { return count++ }; decrement = function() { return count--; }; get = function() { return privateCount; }; })(); this.set = function(param) { if(param >=0 ){ increment(); } else { decrement } }; this.getCount = function() { return get(); } }