The YUI Global Object includes several useful type-checking methods in the Lang
object. In addition to the 'isXYZ' type check methods, YUI 3 includes
Y.Lang.type, which returns a string representing the type of the tested
object. Click the "Check" button in each row to evaluate the data.
| Data | isObject | isArray | isFunction | type |
|---|---|---|---|---|
null |
||||
[] or new Array() |
||||
{} or new Object() |
||||
function Foo() {} |
||||
new Foo() |
||||
Type Checking with YUI
YUI().use('node', function(Y) {
// This method is in the core of the library, so we don't have to use() any
// additional modules to access it. However, this example requires 'node'.
Checking types
In this example, we use a few of the type-checking methods available in
Lang to test various types of data.
// Test the input using Y.Lang type checking methods
var checkType = function (val) {
return {
'object' : Y.Lang.isObject(val),
'array' : Y.Lang.isArray(val),
'function': Y.Lang.isFunction(val),
'type' : Y.Lang.type(val)
};
};
Full Source
YUI().use('node', function(Y) {
// This method is in the core of the library, so we don't have to use() any
// additional modules to access it. However, this example requires 'node'.
var checkType = function (val) {
return {
'object' : Y.Lang.isObject(val),
'array' : Y.Lang.isArray(val),
'function': Y.Lang.isFunction(val),
'type' : Y.Lang.type(val)
};
};
var populateRow = function (e, data) {
var target = e.target;
cell = target.get('parentNode'),
row = cell.get('parentNode');
row.removeChild(cell);
var td0 = document.createElement('td'),
td1 = td0.cloneNode(false),
td2 = td0.cloneNode(false);
td3 = td0.cloneNode(false);
var results = checkType(data);
td0.appendChild(document.createTextNode(results['object'] ? 'Y' : 'N'));
td1.appendChild(document.createTextNode(results['array'] ? 'Y' : 'N'));
td2.appendChild(document.createTextNode(results['function'] ? 'Y' : 'N'));
td3.appendChild(document.createTextNode(results['type']));
row.appendChild(td0);
row.appendChild(td1);
row.appendChild(td2);
row.appendChild(td3);
};
var foo = function () {};
var f = Y.one('#demo');
Y.on('click',populateRow, '#demo-1', Y, null);
Y.on('click',populateRow, '#demo-2', Y, []);
Y.on('click',populateRow, '#demo-3', Y, {});
Y.on('click',populateRow, '#demo-4', Y, foo);
Y.on('click',populateRow, '#demo-5', Y, new foo());
});