lang.mustache revision 7bf968657bff10ad28ae9742176e4e814dbc3849
0N/A<p>`Y.Lang` contains JavaScript language utilities and extensions that are used in the core library.
1879N/A
0N/A
0N/A<h2>Array</h2>
0N/A<p>Testing for an actual `Array`, this helps fight against false positives from `arguments` and `getElementsBy*` array-like structures.</p>
0N/A
0N/A```
0N/A// true, an array literal is an array
0N/AY.Lang.isArray([1, 2]);
0N/A
0N/A// false, an object literal is not an array
0N/AY.Lang.isArray({"one": "two"});
0N/A
0N/A// however, when declared as an array, it is true
0N/Afunction() {
0N/A var a = new Array();
0N/A a["one"] = "two";
0N/A return Y.Lang.isArray(a);
1472N/A}();
1472N/A
1472N/A// false, a collection of elements is like an array, but isn't
0N/AY.Lang.isArray(document.getElementsByTagName("body"));
0N/A```
0N/A
1879N/A<h2>Object</h2>
1879N/A
1879N/A<p>Checking for `Objects`, since `new Object()`, '{}', '[]' and `function(){}` are all technically Objects.</p>
1879N/A
1879N/A```
1879N/A// true, objects, functions, and arrays are objects
1879N/AY.Lang.isObject({});
1879N/AY.Lang.isObject(function(){});
0N/AY.Lang.isObject([1,2]);
0N/A
0N/A// false, primitives are not objects
0N/AY.Lang.isObject(1);
0N/AY.Lang.isObject(true);
0N/AY.Lang.isObject("{}");
0N/A
0N/A```
0N/A
0N/A<h2>Function</h2>
0N/A
0N/A<p>Checking for `Function`, since a `function` is a `function`, but an `Object` is not a function.</p>
0N/A
0N/A```
0N/AY.Lang.isFunction(function(){}); // true
0N/AY.Lang.isFunction({foo: "bar"}); // false
0N/A```
0N/A
0N/A<h2>Number &amp; String</h2>
0N/A
0N/A<p>Making sure that `Strings` are really `Strings` and `Numbers` are really `Numbers`.</p>
0N/A
0N/A```
0N/A// true, ints and floats are numbers
0N/AY.Lang.isNumber(0);
0N/AY.Lang.isNumber(123.123);
0N/A
0N/A// false, strings that can be cast to numbers aren't really numbers
0N/AY.Lang.isNumber("123.123");
0N/A
0N/A// false, undefined numbers and infinity are not numbers we want to use
0N/AY.Lang.isNumber(1/0);
0N/A
0N/A// strings
0N/AY.Lang.isString("{}"); // true
0N/AY.Lang.isString({foo: "bar"}); // false
0N/AY.Lang.isString(123); // false
0N/AY.Lang.isString(true); // false
0N/A```
0N/A
0N/A<h2>Boolean</h2>
0N/A
0N/A<p>Making sure that a `Boolean` is really a `Boolean`.</p>
0N/A
0N/A```
0N/A// true, false is a boolean
0N/AY.Lang.isBoolean(false);
0N/A
0N/A// false, 1 and the string "true" are not booleans
0N/AY.Lang.isBoolean(1);
0N/AY.Lang.isBoolean("true");
0N/A```
0N/A
0N/A<h2>Null &amp; Undefined</h2>
0N/A
0N/A<p>`null` is `null`, but `false`, `undefined` and `""` are not.</p>
0N/A
0N/A```
0N/AY.Lang.isNull(null); // true
0N/AY.Lang.isNull(undefined); // false
0N/AY.Lang.isNull(""); // false
0N/A
0N/A// undefined is undefined, but null and false are not
0N/AY.Lang.isUndefined(undefined); // true
0N/AY.Lang.isUndefined(false); // false
0N/AY.Lang.isUndefined(null); // false
0N/A```
0N/A
0N/A
0N/A