zoukankan      html  css  js  c++  java
  • JavaScript数据类型判断

    原理:利用JavaScript原生原型扩展函数Object.prototype.toString.call

    封装:

    var Type = (function() {
        var type = {};
        var typeArr = ['String', 'Object', 'Number', 'Array','Undefined', 'Function', 'Null', 'Symbol'];
        for (var i = 0; i < typeArr.length; i++) {
            (function(name) {
                type['Is' + name] = function(obj) {
                    return Object.prototype.toString.call(obj) == '[object ' + name + ']';
                 }
             })(typeArr[i]);
         }
         return type;
    })();
    

      

    调用:Type.Is[ 数据类型名称 ]( 需要被判断数据 ) 数据类型:'String', 'Object', 'Number', 'Array','Undefined', 'Function', 'Null', 'Symbol'

        例:Type.IsFunction(function() {}) //true     Type.IsObject(0) /false

    参考链接:点我

  • 相关阅读:
    settTimeout vs setInterval
    JS继承
    JS创建对象
    原型链
    开始学习python的感受
    Problem 29
    Python 查看关键字
    Problem 21
    Problem 34
    Problem 42
  • 原文地址:https://www.cnblogs.com/detanx/p/JavaScriptDataType.html
Copyright © 2011-2022 走看看