หน้าเว็บ

วันพุธที่ 10 เมษายน พ.ศ. 2556

check type javascript


var na5cent = {};

na5cent.Types = (function() {

    function _is(data, type) {
        return Object.prototype.toString.call(data) === '[object ' + type + ']';
    }

    return {

        isFunction: function(data) {
            return _is(data, 'Function');
        },

        isArray: function(data) {
            return _is(data, 'Array');
        },

        isString: function(data) {
            return _is(data, 'String');
        },

        isNumber: function(data) {
            return _is(data, 'Number');
        },

        isObject: function(data) {
            return _is(data, 'Object');
        },

        isRegExp: function(data) {
            return _is(data, 'RegExp');
        }
    };
})();



console.log('na5cent.Types.isFunction(function(){}) => ' + na5cent.Types.isFunction(function() {}));
console.log('na5cent.Types.isFunction("nascent") => ' + na5cent.Types.isFunction('nascent'));
console.log('na5cent.Types.isArray([]) => ' + na5cent.Types.isArray([]));
console.log('na5cent.Types.isArray("nascent") => ' + na5cent.Types.isArray('nascent'));
console.log('na5cent.Types.isString("nascent") => ' + na5cent.Types.isString('nascent'));
console.log('na5cent.Types.isString(5) => ' + na5cent.Types.isString(5));
console.log('na5cent.Types.isNumber("nascent") => ' + na5cent.Types.isNumber('nascent'));
console.log('na5cent.Types.isNumber(2) => ' + na5cent.Types.isNumber(2));
console.log('na5cent.Types.isObject("nascent") => ' + na5cent.Types.isObject('nascent'));
console.log('na5cent.Types.isObject({}) => ' + na5cent.Types.isObject({}));
console.log('na5cent.Types.isRegExp({}) => ' + na5cent.Types.isRegExp({}));
console.log('na5cent.Types.isRegExp(/w/) => ' + na5cent.Types.isRegExp(/w/));
//

ไม่มีความคิดเห็น:

แสดงความคิดเห็น