Using MooTools’ Alias Functionality

By  on  

Not having internet at home for a month gave me a great opportunity to really study the MooTools core JavaScript file. One fun part of the library is its alias() functionality. Alias allows you to do exactly what the word means -- alias one functionality by calling it something else. For example, one usage of alias in the library is:

Hash.alias('forEach', 'each');	//Hash iteration
Array.alias('forEach', 'each');	//Array iteration

Using "each" is shorter and faster to type than "forEach." With aliasing in mind, I thought of a few fun usages of MooTools' alias. All of the following are aliases for the Element.dispose() functionality:


	/* prison house */
	Element.alias('dispose', 'shank');
	$('prisoner').shank();
	
	/* animals */
	Element.alias('dispose', 'beach');
	$('whale').beach();
	
	/* automobiles */
	Element.alias('dispose', 'compound');
	$('car').compound();
	
	/* mafia */
	Element.alias('dispose', 'concreteShoes');
	$('wiseguy').concreteShoes();
	

In all seriousness, I recommend using alias when you want to refer to a specific functionality by giving it your own name.

Recent Features

  • By
    From Webcam to Animated GIF: the Secret Behind chat.meatspac.es!

    My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...

  • By
    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

Incredible Demos

  • By
    Send Email Notifications for Broken Images Using MooTools AJAX

    One of the little known JavaScript events is the image onError event. This event is triggered when an image 404's out because it doesn't exist. Broken images can make your website look unprofessional and it's important to fix broken images as soon as possible.

  • By
    PHP Woot Checker – Tech, Wine, and Shirt Woot

    If you haven't heard of Woot.com, you've been living under a rock. For those who have been under the proverbial rock, here's the plot: Every day, Woot sells one product. Once the item is sold out, no more items are available for purchase. You don't know how many...

Discussion

  1. Great functionality and funny too..!!

  2. Brent Anderson

    So I just got in trouble in a study hall for laughing out loud at "$('prisoner').shank();". That’s hilarious.

  3. Quite funny actually! But how much is this slowing JavaScript down, as it has to go an extra step to check what the function is an alias for?

  4. I never knew that alias existed! I’ve always wanted a short version for domready. Could alias handle that or would be better to just extend Window?

  5. @Ronny-André: The speed difference must be slim to none. MooTools uses ‘.each’ everywhere.

    @Ryan: I don’t believe that alias would be applicable to your example.

  6. This got a chuckle out of me. The .shank example was choice.

    @Ryan, alias just creates an extra pointer to a method, so it doesn’t work with events.

    @Ronny-Andre David is correct; the expense here is nil. It doesn’t invoke some methodology every time you call .each or anything. The alias function just applies it’s logic when it is parsed and after that you have two pointers to the same method.

    As for using this to create your own fun names for things, you certainly could. I think that a far more powerful method for creating your own functionality for native elements is the custom methods that MooTools allows you to define. For instance, look at the element shortcuts in Fx.Tween:

    Element.implement({
        //there are other shortcuts, but let's just look at highlight...
        highlight: function(start, end){
            if (!end){
                end = this.retrieve('highlight:original', this.getStyle('background-color'));
                end = (end == 'transparent') ? '#fff' : end;
            }
            var tween = this.get('tween');
            tween.start('background-color', start || '#ffff88', end).chain(function(){
                this.setStyle('background-color', this.retrieve('highlight:original'));
                tween.callChain();
            }.bind(this));
            return this;
        }
    });
    

    Using implement (combine with element storage) you can create a lot of customized behavior. For instance, let’s say you liked jQuery’s method for adding click events.

  7. Damned formatting. I indented that crap!

  8. Goutte

    Note that the parameters have been reversed in newer Mootools (using 1.3.2) versions :

    Element.alias('shank', 'dispose');
    Element.alias('concreteShoes', 'dispose');

    etc.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!