Download code

I’ve created a few JavaScript functions that I find tend to come in handy when programming. I call them “extensions” because they extend the base window object with additional functions. I debated on namespacing these, but find that either they are used often enough that having to type a namespace every time becomes annoying, or the code is more readable without the namespace. Also, these are functions that are useful in their own right, outside of any library, so namespacing them to a library didn’t seem right to me. (If you are not familiar with namespacing in JavaScript don’t worry about it. I’ll discuss it in an upcoming post.)

Documentation is included in the source code, but below is a quick reference, so you can see what’s in there. All of these functions are added to the global window object, so you can use them directly. For example, to copy a JavaScript object, you can use:

1
2
3
4
5
6
7
8
source = {
  firstName: 'Jane',
  lastName: 'Doe',
  birthDate: new Date(2000, 10, 15)
};
 
// Use the copy function to create a duplicate of source
var dest = copy(source);

Or, to check if an object is an array, you can use one of the following methods:

1
2
3
4
5
6
7
var arr = [1,2,3];
 
if (isArray(arr))
  alert('It is an array!');
 
if (typeOf(arr) == 'array')
  alert('It is an array!');

This is only a first pass at these functions. In an upcoming post, I will be adding more extensions to this library. This will include adding some useful functions to the basic data types like String, Number, Date and Array. More to come, so stay tuned.


typeOf(obj)

Same as the built-in typeof operator, but distinguishes
arrays and dates from objects.

Params:
obj {object} The object to get the type of.
Returns:
{string} The name of the type of the object.


isUndefined(obj)

Checks if the object passed in is undefined.

Params:
obj {object} The object to be checked.
Returns:
True if obj is undefined, and false otherwise.


isNull(obj)

Checks if the object passed in is null.

Params:
obj {object} The object to be checked.
Returns:
True if obj is null, and false otherwise.


isString(obj)

Checks if the type of object passed in is a string.

Params:
obj {object} The object to be checked.
Returns:
True if obj is a string, and false otherwise.


isNumber(obj)

Checks if the type of object passed in is a number.

Params:
obj {object} The object to be checked.
Returns:
True if obj is a number, and false otherwise.


isBoolean(obj)

Checks if the type of object passed in is a boolean.

Params:
obj {object} The object to be checked.
Returns:
True if obj is a boolean, and false otherwise.


isDate(obj)

Checks if the type of object passed in is a date. This function will
distinguish between an object and a date.

Params:
obj {object} The object to be checked.
Returns:
True if obj is a date, and false otherwise.


isArray(obj)

Checks if the type of object passed in is an array. This function will
distinguish between an object and an array.

Params:
obj {object} The object to be checked.
Returns:
True if obj is an array, and false otherwise.


isObject(obj)

Checks if the type of object passed in is an object. This function will distinguish between an array and date vs. an object.

Params:
obj {object} The object to be checked.
Returns:
True if obj is an array, and false otherwise.


isFunction(obj)

Checks if the type of object passed in is a function.

Params:
obj {object} The object to be checked.
Returns:
True if obj is a function, and false otherwise.


copy(source, [dest])

Copies the properties of one object to another (including inner objects, arrays and non-prototype functions). Existing properties in the destination object are preserved, but will be overridden if those properties also exist in the source. Any functions in the prototype of the source will NOT be copied. If no destination object is passed in, a destination object will be created.

Params:
src {object} Source object that properties are to be copied from.
dest {object} Destination object that properties are to be copied to.
Returns:
The destination object.


scopedFn(scope, func)

Used to create a function pointer that will always be called against a specific object. Can be especially useful when assigning event handlers to DOM objects and you want the “this” variable to point to your own object rather than the DOM object.

Params:
{object} scope The object on which func is to be called.
func {function} The function to be called.
Returns:
A scope-corrected function that ensures func is always called against the given scope object.


emptyFn()

Shorthand for an empty function.