Skip to content

Instantly share code, notes, and snippets.

@jaredatron
Created March 24, 2010 23:02
Show Gist options
  • Save jaredatron/342934 to your computer and use it in GitHub Desktop.
Save jaredatron/342934 to your computer and use it in GitHub Desktop.
function Hash(object){
function hash(key, value){
return (
arguments.length === 2 ? hash.set(key, value) :
arguments.length === 1 ? hash.get(key) :
hash.index
);
};
hash.index = {};
for (var p in Hash.prototype) hash[p] = Hash.prototype[p];
return hash.extend(object);
}
Hash.prototype.get = function get(key){
return this.index[key];
};
Hash.prototype.set = function get(key, value){
this.index[key] = value;
return this;
};
Hash.prototype.extend = function extend(object){
for (var p in object) this.index[p] = object[p];
return this;
};
Hash.prototype.toObject = function toObject(){ return this.index; };
var things = new Hash({name:'fred'});
print( things('name') );
print( things().name );
things('foo', 'bar');
print( things('foo') );
print( things().foo );
things().foo = 'baz';
print( things('foo') );
print( things().foo );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment