YUI 3.x Home -

YUI Library Examples: The YUI Global Object: Check Data Types with Lang

The YUI Global Object: Check Data Types with Lang

The YUI Global Object includes several useful type-checking methods in the Lang object. In addition to the 'isXYZ' type check methods, YUI3 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

Instantiate YUI

  1. <!-- include yui -->
  2. <script type="text/javascript" src="../../build/yui/yui.js"></script>
  3.  
  4. YUI().use("node", function(Y) {
  5. // This method is in the core of the library, so we don't have to use() any
  6. // additional modules to access it. However, this example requires 'node'.
<!-- include yui -->
<script type="text/javascript" src="../../build/yui/yui.js"></script>
 
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.

  1. // Test the input using Y.Lang type checking methods
  2. var checkType = function (val) {
  3. return {
  4. 'object' : Y.Lang.isObject(val),
  5. 'array' : Y.Lang.isArray(val),
  6. 'function': Y.Lang.isFunction(val),
  7. 'type' : Y.Lang.type(val)
  8. };
  9. };
// 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

  1. YUI().use("node", function(Y) {
  2. // This method is in the core of the library, so we don't have to use() any
  3. // additional modules to access it. However, this example requires 'node'.
  4.  
  5. var checkType = function (val) {
  6. return {
  7. 'object' : Y.Lang.isObject(val),
  8. 'array' : Y.Lang.isArray(val),
  9. 'function': Y.Lang.isFunction(val),
  10. 'type' : Y.Lang.type(val)
  11. };
  12. };
  13.  
  14. var populateRow = function (e, data) {
  15. var target = e.target;
  16. cell = target.get('parentNode'),
  17. row = cell.get('parentNode');
  18.  
  19. row.removeChild(cell);
  20.  
  21. var td0 = document.createElement('td'),
  22. td1 = td0.cloneNode(false),
  23. td2 = td0.cloneNode(false);
  24. td3 = td0.cloneNode(false);
  25.  
  26. var results = checkType(data);
  27.  
  28. td0.appendChild(document.createTextNode(results['object'] ? 'Y' : 'N'));
  29. td1.appendChild(document.createTextNode(results['array'] ? 'Y' : 'N'));
  30. td2.appendChild(document.createTextNode(results['function'] ? 'Y' : 'N'));
  31. td3.appendChild(document.createTextNode(results['type']));
  32.  
  33. row.appendChild(td0);
  34. row.appendChild(td1);
  35. row.appendChild(td2);
  36. row.appendChild(td3);
  37. };
  38.  
  39. var foo = function () {};
  40. var f = Y.one('#demo');
  41.  
  42. Y.on('click',populateRow, '#demo-1', Y, null);
  43. Y.on('click',populateRow, '#demo-2', Y, []);
  44. Y.on('click',populateRow, '#demo-3', Y, {});
  45. Y.on('click',populateRow, '#demo-4', Y, foo);
  46. Y.on('click',populateRow, '#demo-5', Y, new foo());
  47.  
  48. });
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());
 
});

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings