Jump to Table of Contents

Y.Lang

Y.Lang contains JavaScript language utilities and extensions that are used in the core library.

Array

Testing for an actual Array, this helps fight against false positives from arguments and getElementsBy* array-like structures.

// true, an array literal is an array
Y.Lang.isArray([1, 2]);

// false, an object literal is not an array
Y.Lang.isArray({"one": "two"});

// however, when declared as an array, it is true
function() {
    var a = new Array();
    a["one"] = "two";
    return Y.Lang.isArray(a);
}();

// false, a collection of elements is like an array, but isn't
Y.Lang.isArray(document.getElementsByTagName("body"));

Object

Checking for Objects, since new Object(), '{}', '[]' and function(){} are all technically Objects.

// true, objects, functions, and arrays are objects
Y.Lang.isObject({});
Y.Lang.isObject(function(){});
Y.Lang.isObject([1,2]);

// false, primitives are not objects
Y.Lang.isObject(1);
Y.Lang.isObject(true);
Y.Lang.isObject("{}");

Function

Checking for Function, since a function is a function, but an Object is not a function.

Y.Lang.isFunction(function(){}); // true
Y.Lang.isFunction({foo: "bar"}); // false

Number & String

Making sure that Strings are really Strings and Numbers are really Numbers.

// true, ints and floats are numbers
Y.Lang.isNumber(0);
Y.Lang.isNumber(123.123);

// false, strings that can be cast to numbers aren't really numbers
Y.Lang.isNumber("123.123");

// false, undefined numbers and infinity are not numbers we want to use
Y.Lang.isNumber(1/0);

// strings
Y.Lang.isString("{}"); // true
Y.Lang.isString({foo: "bar"}); // false
Y.Lang.isString(123); // false
Y.Lang.isString(true); // false

Boolean

Making sure that a Boolean is really a Boolean.

// true, false is a boolean
Y.Lang.isBoolean(false);

// false, 1 and the string "true" are not booleans
Y.Lang.isBoolean(1);
Y.Lang.isBoolean("true");

Null & Undefined

null is null, but false, undefined and "" are not.

Y.Lang.isNull(null); // true
Y.Lang.isNull(undefined); // false
Y.Lang.isNull(""); // false

// undefined is undefined, but null and false are not
Y.Lang.isUndefined(undefined); // true
Y.Lang.isUndefined(false); // false
Y.Lang.isUndefined(null); // false