Skip navigation.

Benjamin Joffe

Very useful JavaScript snippet

/**
 * Copyright (c) 2009, Benjamin Joffe
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN FACT, YOU REALLY SHOULD ONLY USE THIS CODE IF YOU ARE BAT
 * SHIT CRAZY.
 */

if (window['Boolean'] && window['Boolean']['prototype']) new function()
{
    function extend(obj, prop, funct)
    {
        try {
            obj['prototype'][prop] = funct;
        }
        catch(ignore){}
    }
    function funct()
    {
        if (this instanceof Boolean)
        {
            switch (true)
            {
                case this == true : return true;
                case this != true : return !true;
            }
        }
        throw new Error('Unknown type: '+this);
    }
    function wrap()
    {
        if (this == true)
        {
            return window.Boolean.prototype.isTrue.apply(false, []);
        }
        else
        {
            return window.Boolean.prototype.isTrue.apply(true, []);
        }
    }
    extend(window['Boolean'], 'isTrue', funct);
    extend(window['Boolean'], 'isFalse', wrap);
}

On a more serious note, is there any useful purpose for extending the Boolean prototype?

Europe 20073D Earth with Canvas.

Write a comment

You must be logged in to write a comment. If you're not a registered member, please sign up.

Download Opera, the fastest and most secure browser
December 2009
M T W T F S S
November 2009January 2010
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31