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]";
    }
  • 相关阅读:
    90.子类调用父类同名方法总结
    89.子类调用父类同名属性和方法
    88.子类调用父类同名属性和方法
    87.子类重写父类的同名属性和方法
    86.多继承
    85.单继承
    84.继承的概念
    day09
    83.魔法方法__del__()
    82.魔法方法__str__()
  • 原文地址:https://www.cnblogs.com/oceanden/p/4185450.html
Copyright © 2011-2022 走看看