Some javascript functions...
As all of us know javascript is the mostly used script language for web development. But we cann't say it is perfect one. Let see some functions that will help us to avoid some javascript problems....
typeof(a)
Though the typeof operator can be used to find the type of objects,
it has got some problems. so we can use the following global functions to wrap
the typeof operator.
isIEObject(a)
Internet Explorer holds references to objects that are not actually javascript
objects. So if we use the objects in javascript it will give error. But the
typeof operator identifies them as javascript objects( problem!!!). Here we can
use the isIEObject() function to identify those
objects.Script:function isIEObject(a)
{
return isObject(a) && typeof a.constructor != 'function';
}
isArray(a)This function returns true if a is an
array, meaning that it was produced by the Array constructor or by the [ ]
array literal notation.Script:function isArray(a)
{
return isObject(a) && a.constructor == Array;
}
isBoolean(a)
This function returns true if a is one of the Boolean values, true or
false.
Script:function isBoolean(a)
{
return typeof a == 'boolean';
}
isEmpty(a)
This function returns true if a is an object or array or function containing no
enumerable members.Script:function isEmpty(o)
{
if (isObject(o))
{
for (var i in o)
{
return false;
}
}
return true;
}
isFunction(a)
This function returns true if a is a function. Beware that some native
functions in IE were made to look like objects instead of functions. This
function does not detect that.Netscape is better behaved in this regard.
Script:function isFunction(a)
{
return typeof a == 'function';
}
isNull(a)
This function returns true if a is the null value.Script:function isNull(a)
{
return typeof a == 'object' && !a;
}
isNumber(a)This function returns true if a is
a finite number. It returns false if a is NaN or Infinite. It also returns
false if a is a string that could be converted to a number.
Script:function isNumber(a)
{
return typeof a == 'number' && isFinite(a);
}
isObject(a)
This function returns true if a is an object, array, or function. It returns
false if a is a string, number, Boolean, null, or
undefined.Script:function
isObject(a)
{
return (typeof a == 'object' && !!a) || isFunction(a);
}
isString(a)
This function returns true if a is a string.
Script:function isString(a)
{
return typeof a == 'string';
}
isUndefined(a)This function returns true if a is the
undefined value. You can get the undefined value from an uninitialized variable
or from an object's missing member.
Script:function isUndefined(a)
{
return typeof a == 'undefined';
}