Skip to content

Instantly share code, notes, and snippets.

@ELLIOTTCABLE
Created August 24, 2009 02:51
Show Gist options
  • Save ELLIOTTCABLE/173621 to your computer and use it in GitHub Desktop.
Save ELLIOTTCABLE/173621 to your computer and use it in GitHub Desktop.
// In this example, the private_thing is more like a private class variable than a private instance variable.
thingie_proto = function(){
thingie_proto = {};
var private_thing = '';
thingie_proto['private_thing'] = function(){ return private_thing; }; // get…
thingie_proto['set_private_thing'] = function(o){ private_thing = o; }; // set…
thingie_proto['public_thing'] = '';
return thingie_proto;
}();
var F=function(){};
F.prototype = thingie_proto;
thingie_one = new F();
thingie_two = new F();
thingie_one['public_thing'] = 'dog';
thingie_two['public_thing'] = 'cat';
thingie_one.set_private_thing('house');
thingie_two.set_private_thing('car');
puts(thingie_one['public_thing']);
puts(thingie_two['public_thing']);
puts(thingie_one.private_thing());
puts(thingie_two.private_thing());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment