What's New in 1.2: The Hash

October 8th, 2007, posted by Tom

In recent development of MooTools, we've incorporated into the core a plugin that was once pushed under the table and neglected. The Hash is a powerful new way to extend objects just like you've always wanted...

Alright, what the heck is all this Hash business?

The current 1.2 development version of MooTools saw a promotion of the Hash Class up from a plugin to a Native type. A Hash is basically a wrapper for native JavaScript Objects. The reason it was created was because as we all know, the Object prototype is off limits! (for a brief explanation... see rule #2). So what can i do with this crazy new thing?

var hash = new Hash({ 'a': 'one', 'b': 'two', 'c': 'three', 'd': 'four'});
hash.get('a'); //"one"
hash.set('e', 'five'); //hash.e is now 'five'
hash.set('c', 'new value'); //hash.c is now 'new value'
hash.f = 'another value here'; //this is perfectly acceptable also, get / set are just shortcuts to filter out prototypes
hash.has('b') //true
hash.each(function(value, key){ alert(key + ':' + value); }); //alerts a:one, b:two, etc.
hash.get('each'); //null

Now i know what you're thinking, "Alright, so what... i could do all that stuff before with Objects, even the each with a simple for in loop." Well, no. The Hash Native also supports just about every Array method including 'map', 'every', 'some', and 'filter', as well as a few other really handy shortcuts. We now use Hash as the all-purpose data type throughout the MooTools Framework. Once you start using it, you'll know why. ;) (Also a note to anyone who has ever used Abstract in MooTools... The Hash is a replacement for Abstract and is much, much more powerful. Anywhere you used to have Abstract in your 1.1+ MooTools code, just replace with Hash and you should be all good)

Generics are so cool...

Just like all other MooTools Natives, Hash has full generics support. This means that any function you can call on a hash, you can also call by passing the hash as the first argument to the genericized function. Also, any generic you can use on a hash can also be used on a native JavaScript Object.

var obj = { 'a': 'one', 'b': 'two', 'c': 'three', 'd': 'four'};
Hash.each(obj, function(value, key){ alert(key + ':' + value); }); //alerts a:one, b:two, etc.
Hash.filter(obj, function(value, key){ return key.test(/a|c/); }); //returns a hash containing { 'a': 'one', 'c': 'three' }
Hash.getKeys(obj); //returns ['a', 'b', 'c', 'd']
What other stuff can i do?

Well perhaps the most powerful aspect of Hashes is as i mentioned before, all the Array functions you've come to know and love can now be used on Hashes. Check out the source for many more implemented methods. Hash really is a awesome tool, so wonderful in fact that it's been included in the Core of the framework. In my next article, I'll talk about some exciting changes to Fx in MooTools, as well as some awesome new Element shortcuts.

--ciao for now!

12 Responses to “What's New in 1.2: The Hash”

  1. Josh Says:

    Ooohhhh SWEET!

    I’ve been waiting for this for a while - I can’t wait to get my hands on this baby!!

    Great job!

    J

  2. niko Says:

    i could do this:

    Object.prototype.each = function() ….

    whats the advantage of your Hash-Object?

  3. Please do use Object.prototype.each and enjoy breaking all of MooTools.

    Check out an explanation about why playing with Object.prototype is bad: http://erik.eae.net/archives/2005/06/06/22.13.54

  4. Anto Says:

    Good job.

  5. tofu Says:

    i’ve been keeping to read thru the source on dev once in a while. 1.2 is goin’ to be awesome! thx alot for your effort and time you spend on developing this framework. greetings from switzerland, tofu

  6. r4zv4n Says:

    Oh boy, oh boy :)

    Can’t wait for the 1.2 final.

  7. seventhapex Says:

    Word!

  8. sandro Says:

    Great blog post, definitely looking forward to more in the future, 1.2 is going to be awesome!

  9. Eric Rogé Says:

    I may have not understood the Hash concept, but all these fonctionnalities aren’t they natively avaible ?

    You say : var hash = new Hash({ ‘a’: ‘one’, ‘b’: ‘two’, ‘c’: ‘three’, ‘d’: ‘four’}); I say : var myObj = { ‘a’: ‘one’, ‘b’: ‘two’, ‘c’: ‘three’, ‘d’: ‘four’};

    You say : hash.get(‘a’); //”one” I say : myObj.a; //”one”

    You say : hash.set(‘e’, ‘five’); //hash.e is now ‘five’ I say : myObj.a = ‘five’; //myObj.e is now ‘five’

    You say : hash.has(‘b’)//true I say : myObj.b != undefined //true

    You say : hash.each(function(value, key){ alert(key + ‘:’ + value); }); //alerts a:one, b:two, etc. I say : for (var i in myObj) { alert(i + ‘:’ + obj[i]); } //alerts a:one, b:two, etc.

    You say : Hash.filter(obj, function(value, key){ return key.test(/a|c/); }); //returns a hash containing { ‘a’: ‘one’, ‘c’: ‘three’ } I say : var filteredObj; for (var i in myObj) { if(i == ‘a’ || i == ‘c’){ filteredObj[i] = obj[i]; } } // filteredObj contains { ‘a’: ‘one’, ‘c’: ‘three’ }

    You say : Hash.getKeys(obj); //returns [‘a’, ‘b’, ‘c’, ‘d’] I say : var myObjKeys = []; for (var i in myObj) { myObjKeys.push(i); }//myObjKeys contains [‘a’, ‘b’, ‘c’, ‘d’]

    PS : excuse my poor english.

  10. Right Eric, they are natively availible… and their used a lot.

    Especially throughout MooTools, they’re used so much, that we decided to abstract all the functionality out to make it easier to get the same job done in a lot less code.

    Also, we are able to abide by the OO principles that exist throughout the rest of the framework by giving Hash instances the functionality we do.

    –cheers :)

  11. Valerio Says:

    Hello Eric,

    Thats really a weak argument. Just because you can do it with more code, it doesnt mean its not “needed”. If you think that way, you dont need a framework at all, and you might as well use all the “natively available” functionalities.

    Plus, MooTools in all of these methods checks for hasOwnProperty, to make it impossible for you to overwrite/get/set/check for an Hash prototype.

    Hope this clears the confusion.

    Cheers!

  12. Eric Rogé Says:

    Ok guys, it sounds great.

Sorry, comments are closed for this article.