zoukankan      html  css  js  c++  java
  • javascript常用判断函数

    /****************************************
        检测是否是数组对象
    *****************************************/
    var isArray = Function.isArray || function(o){
        return typeof o === 'object' && 
        Object.prototype.toString.call(o) === '[object Array]';
    }
    //判定o是否是一个类数组对象
    //字符串和函数有length属性,但是它们可以用typeof检测将其排除。
    //在客户端javascript中,DOM文本节点也有length属性,需要用额外判断o.nodeType != 3将其排除
    function isArrayLike(o){
        if(o &&                                        //o非null、undefined等
            typeof o === 'object' &&                //o是对象
            isFinite(o.length) &&                    //o.length是有限数值
            o.length >= 0 &&                        //o.length是非负值
            o.length === Math.floor(o.length) &&    //o.length是整数
            o.length < 4294967296){                    //o.length < 2^32
            return true;                            //o是类数组对象
        }else{
            return false;                            //否则它不是
        }
    }
    //classof()函数,返回传递给它的任意对象的类
    function classof(o){
        if(o === null){
            return "Null";
        };
        if(o === undefined){
            return "Undefined";
        };
        return Object.prototype.toString.call(o).slice(8, -1);
    }
    /****************************************
        检测是否是函数对象
    *****************************************/
    function isFunction(x){
        return Object.prototype.toString.call(x) === "[object Function]";
    }
  • 相关阅读:
    Python ES操作
    SVN总结
    MongoDB问题总结
    MySQL
    PyQt小工具
    Python logging模块
    shell脚本
    cmd命令
    eclipse java 项目打包
    Robot Framework:failed: Data source does not exist.错误
  • 原文地址:https://www.cnblogs.com/oceanden/p/4185450.html
Copyright © 2011-2022 走看看