Language Utilities. Easier inheritance, scope handling, type checks.

Table of Contents #

deepClone(val, [instanceClone]):* #

Deep clone native types like Object, Array, RegExp, Date and primitives.

The instanceClone function will be invoked to clone objects that are not "plain" objects (as defined by isPlainObject) if it is provided. If instanceClone is not specified, it will not attempt to clone non-plain objects, and will copy the object reference.


var a = {foo:'bar', obj: {a:1, b:2}};
var b = deepClone(a); // {foo:'bar', obj: {a:1, b:2}}
console.log( a === b ); // false
console.log( a.obj === b.obj ); // false

var c = [1, 2, [3, 4]];
var d = deepClone(c); // [1, 2, [3, 4]]
var e = c.concat(); // [1, 2, [3, 4]]

console.log( c[2] === d[2] ); // false
// concat doesn't do a deep clone, arrays are passed by reference
console.log( e[2] === d[2] ); // true

function Custom() { }
function cloneCustom(x) { return new Custom(); }
var f = { test: new Custom() };
var g = deepClone(f, cloneCustom);
g.test === f.test // false, since new Custom instance will be created

createObject(parent, [props]):Object #

Create Object using prototypal inheritance and setting custom properties.

Mix between Douglas Crockford Prototypal Inheritance and the EcmaScript 5 Object.create() method.


  1. parent (Object) : Parent Object
  2. [props] (Object) : Object properties


var base = {
    trace : function(){

var myObj = createObject(base, {
    name : 'Lorem Ipsum'

myObject.trace(); // "Lorem Ipsum"

ctorApply(constructor, args):Object #

Do Function.prototype.apply() on a constructor while maintaining prototype chain.

function Person(name, surname) { = name;
    this.surname = surname;

Person.prototype.walk = function(){
    console.log( +' is walking');

var args = ['John', 'Doe'];

// "similar" effect as calling `new Person("John", "Doe")`
var john = ctorApply(Person, args);
john.walk(); // "John is walking"

defaults(val, ...defaults):void #

Return first value that isn't null or undefined.

function doSomethingAwesome(foo, bar) {
    // default arguments
    foo = defaults(foo, 'lorem');
    bar = defaults(bar, 123);
    // ...

inheritPrototype(child, parent):void #

Inherit prototype from another Object.

function Foo(name){ = name;
Foo.prototype = {
    getName : function(){

function Bar(name){ = name;
//should be called before calling constructor
inheritPrototype(Bar, Foo);

var myObj = new Bar('lorem ipsum');
myObj.getName(); // "lorem ipsum"

isArguments(val):Boolean #

If value is an "Arguments" object.

isArray(val):Boolean #

If value is an Array. Uses native ES5 Array.isArray() if available.

isBoolean(val):Boolean #

If value is a Boolean.

isDate(val):Boolean #

If value is a Date.

isEmpty(val):Boolean #

Checks if Array/Object/String is empty.

isEmpty('');         // true
isEmpty('bar');      // false
isEmpty([]);         // true
isEmpty([1, 2]);     // false
isEmpty({});         // true
isEmpty({a:1, b:2}); // false

isFinite(val):Boolean #

Checks if value is Finite.

Note: This is not the same as native isFinite, which will return true for booleans and empty strings. See

isFunction(val):Boolean #

If value is a Function.

isKind(val, kind):Boolean #

If value is of "kind". (used internally by some of the isSomething checks).

Favor the other methods since strings are commonly mistyped and also because some "kinds" can only be accurately checked by using other methods (e.g. Arguments), some of the other checks are also faster.

isKind([1,2], 'Array'); // true
isKind(3, 'Array');     // false
isKind(3, 'Number');    // true

See: kindOf()

isNaN(val):Boolean #

Check if value is NaN.

Note: This is not the same as native isNaN, which will return true for undefined and other values. See ES5 isNaN

isNull(val):Boolean #

If value is null.

isNumber(val):Boolean #

If value is a Number.

isObject(val):Boolean #

If value is an Object.

isPlainObject(val):Boolean #

If the value is an Object created by the Object constructor.

isRegExp(val):Boolean #

If value is a RegExp.

isString(val):Boolean #

If value is a String.

isUndefined(val):Boolean #

If value is undefined.

kindOf(val):String #

Gets kind of value (e.g. "String", "Number", "RegExp", "Null", "Date"). Used internally by isKind() and most of the other isSomething checks.

kindOf([1,2]); // "Array"
kindOf('foo'); // "String"
kindOf(3);     // "Number"

See: isKind()

toArray(val):Array #

Convert array-like object into Array or wrap value into Array.

    "0" : "foo",
    "1" : "bar",
    "length" : 2
});                              // ["foo", "bar"]

function foo(){
    return toArray(arguments);
foo("lorem", 123);               // ["lorem", 123]

toArray("lorem ipsum");          // ["lorem ipsum"]
toArray(window);                 // [window]
toArray({foo:"bar", lorem:123}); // [{foo:"bar", lorem:123}]

See: object/values()

For more usage examples check specs inside /tests folder. Unit tests are the best documentation you can get...

Documentation generated by mdoc.