zoukankan      html  css  js  c++  java
  • js中推断对象详细类型

    大家可能知道js中推断对象类型能够用typeof来推断。

    看以下的情况

    <script>
               alert(typeof 1);//number
               alert(typeof "2");//string
               alert(typeof [1,2,3]);//object
               alert(typeof {"name":"zhuhui"})//object
    </script>

    从上面中我们能够看出数组和普通对象用typeof推断出来都是object,可是如今我们有这个需求,我们要明白推断出这个对象是详细的哪个对象(比方数组对象,日期对象。正則表達式对象。其它自己定义对象,DOM对象等等)那怎么办呢。事实上js中有个方法能够准备的推断出

    Object.prototype.toString.call
               var type=function(v){
            	   return Object.prototype.toString.call(v);
               };
               alert(type(null));//[object Null]
               alert(type(undefined));//[object Undefined]
               alert(type(1));//[object Number]
               alert(type(true));//[object Boolean]
               alert(type("2"));//[object String]
               alert(type([1,2,3]));//[object Array]
               alert(type({"name":"zhuhui"}));//[object Object]
               alert(type(type));//[object Function]
               alert(type(new Date()));//[object Date]
               alert(type(/^d+$/));//[object Regexp]
    关于这种方法更深入的说明请參考http://www.cnblogs.com/ziyunfei/archive/2012/11/05/2754156.html

    以下是曾经做项目封装推断常见数据类型的js

    /**
     * type.js 数据类型检測函数
     * @author 朱辉
     * @email javaee6@qq.com
     * @version 0.1
     */
    (function(window, undefined){
        xjo = window.xjo ||
        {
            plugin: {}
        };
        xjo.type = {};
        //检測v的类型 辅助函数
        var type = function(v){
            return Object.prototype.toString.call(v);
        };
        
        /**
         * 是否为数组对象类型  假设是就返回true 假设不是就返回false
         * @namespace xjo.type
         * @method isArray
         * @param {Any} v 被检測的变量
         * @return {Boolean} 结果
         */
        xjo.type.isArray = function(v){
            return type(v) === '[object Array]';
        };
        /**
         * 是否为參数管理器Arguments 假设是就返回true 假设不是就返回false
         * @param {Any} v 被检測的变量
         * @return {Boolean}
         */
        xjo.type.isArguments = function(v){
            return v.callee != undefined;
        };
        /**
         * 是否为迭代序列 包括Array与Arguments 假设是就返回true 假设不是就返回false
         * @param {Any} v 被检測的变量
         * @return {Boolean}
         */
        xjo.type.isIterable = function(v){
            return xjo.type.isArray(v) || xjo.type.isArguments(v);
        };
        /**
         * 是否为空对象 null和undefined和数组的长度为0或空字符串("") 假设是就返回true 假设不是就返回false
         * @param {Any} v 被检測的变量 
         * @param {Boolean} allowBlank [可选] 默认false 空字符串觉得是空对象 反之 空字符串不觉得是空对象
         * @return {Boolean}
         */
        xjo.type.isEmpty = function(v, allowBlank){
            return v === null || v === undefined ||
            (xjo.type.isArray(v) && !v.length) ||
            (!allowBlank ? v === '' : false);
        };
        /**
         * 是否为字符串类型 假设是就返回true 假设不是就返回false
         * @param {Any} v 被检測的变量
         * @return {Boolean}
         */
        xjo.type.isString = function(v){
            return typeof v === 'string';
        };
        /**
         * 是否为数字类型(为Number且不为正负无穷大数字) 假设是就返回true 假设不是就返回false
         * @param {Any} v 被检測的变量
         * @return {Boolean}
         */
        xjo.type.isNumber = function(v){
            return typeof v === 'number' && isFinite(v);
            
        };
        /**
         * 是否为布尔值类型  假设是就返回true 假设不是就返回false
         * @param {Any} v 被检測的变量
         * @return {Boolean}
         */
        xjo.type.isBoolean = function(v){
            return typeof v === 'boolean';
        };
        /**
         * 是否为函数类型 假设是就返回true 假设不是就返回false
         * @param {Any} v 被检測的变量
         * @return {Boolean}
         */
        xjo.type.isFuntion = function(v){
            return type(v) === '[object Function]';
        };
        /**
         * 是否为对象类型
         * @param {Any} v 被检測的变量
         * @return {boolean}
         */
        xjo.type.isObject = function(v){
            return !!v && type(v) === '[object Object]';
        };
        /**
         * 是否为日期类型  假设是就返回true 假设不是就返回false
         * @param {Any} v 被检測的变量
         * @return {boolean}
         */
        xjo.type.isDate = function(v){
            return type(v) === '[object Date]';
        };
        /**
         * 是否为正則表達式类型  假设是就返回true 假设不是就返回false
         * @param {Any} v 被检測的变量
         * @return {Boolean}
         */
        xjo.type.isRegexp = function(v){
            return type(v) == '[object RegExp]';
        };
        /**
         * 是否为原始数据类型 假设是就返回true 假设不是就返回false
         * @param {Any} v 被检測的变量
         * @return {Boolean}
         */
        xjo.type.isPrimitive = function(v){
            return xjo.type.isString(v) || xjo.type.isNumber(v) ||
            xjo.type.isBoolean(v);
        };
        /**
         * 返回数据类型的字符串形式<br>
         *  数字类型:"number" <br>
         *  布尔类型:"boolean" <br>
         *  字符串类型:"string" <br>
         *  数组类型:"array"<br>
         *  日期类型:"date"<br>
         *  正則表達式类型:"regexp" <br>
         *  函数类型:"function"<br>
         *  对象类型:"object"<br>
         *  參数管理器类型:"arguments"<br>
         *  其它类型:"unknow"
         * @param {Any} v 被检測的变量
         * @return {String}
         */
        xjo.type.type = function(v){
            var result = "unknow";
            if (xjo.type.isNumber(v)) {
                result = "number";
            }
            if (xjo.type.isBoolean(v)) {
                result = "boolean";
            }
            if (xjo.type.isString(v)) {
                result = "string";
            }
            if (xjo.type.isArray(v)) {
                result = "array";
            }
            if (xjo.type.isDate(v)) {
                result = "date";
            }
            if (xjo.type.isRegexp(v)) {
                result = "regexp";
            }
            if (xjo.type.isFuntion(v)) {
                result = "function";
            }
            if (xjo.type.isObject(v)) {
                result = "object";
            }
            if (xjo.type.isArguments(v)) {
                result = "argumetns";
            }
            return result;
        };
        xjo.plugin["jo/type"] = true;
    })(window);



  • 相关阅读:
    【activemq artemis】消息持久化——文件系统以及jdbc
    【activemq artemis】安全相关配置
    【微信原生支付】服务商模式-小微商户专属接口:小微商户新增对应APPID关联API
    【activemq artemis】新一代ActiveMQ —— Apache ActiveMQ Artemis
    【签名加解密】c# 对XML进行数字签名并且让java验签成功
    自省书!!!!!
    大前端完整学习路线(详解)//转载自csdn:http://blog.csdn.net/u011047006/article/details/52597178
    Javascript学习十
    Javascript学习九
    Javascript学习八
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5093788.html
Copyright © 2011-2022 走看看