首页 > Web开发 > 详细

js判断变量的类型(使用闭包来玩一把)

时间:2017-07-03 11:29:09      阅读:288      评论:0      收藏:0      [点我收藏+]
        var Type = (function() {
            var Type = {};

            for (var i = 0, type; type = [‘Undefined‘, ‘Null‘, ‘Boolean‘, ‘Number‘, ‘String‘, ‘Function‘, ‘Array‘, ‘Object‘][i++]; ) {

                (function(type) {
                    Type[‘is‘ + type] = function(obj) {
                        return Object.prototype.toString.call(obj) === ‘[object ‘ + type + ‘]‘;
                    }; 
                })(type);

            };

            return Type;
        })();

        console.log(Type.isUndefined());    // true
        console.log(Type.isNull(a = null));    // true
        console.log(Type.isBoolean(false));    // true
        console.log(Type.isNumber(123));    // true
        console.log(Type.isString(‘str‘));    // true
        console.log(Type.isFunction(function() {}));   // true
        console.log(Type.isArray([1, 2, 3]));    // true
        console.log(Type.isObject({}));    // true

 

js判断变量的类型(使用闭包来玩一把)

原文:http://www.cnblogs.com/sorrowx/p/7109961.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!