Very useful JavaScript snippet
Thursday, 30. July 2009, 10:35:48
/**
* 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?















