Created
March 24, 2010 23:02
-
-
Save jaredatron/342934 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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