zoukankan      html  css  js  c++  java
  • js 判断数据类型的方法及实现

    转载自 http://blog.csdn.net/xujiaxuliang/archive/2009/10/21/4708353.aspx  

    null 与 undefined 区别: null 是js的保留字(独一无二的值),可以理解为无对象(因为typeof null =='object'),当null在布尔环境下时,转为false, 当在数字环境时转为0,当在字符串环境时转为‘null’;undefined是js实现的全局变量,是指未声明的变量,或者是使用已经声明但未赋值的变量,或者是使用了一个并不存在的对象属性;当undefined 在布尔环境下时,转为false, 当在数字环境时转为NaN,当在字符串环境时转为‘undefined ’;

    (1) typeof: 为了从对象中区分出基本类型的。但数组是 Object , 函数是 function , undefined 是undefined , null 是 object。
    (2) Object.prototype.toString.call :是指使用最原始的 Object 的原型对象 prototype 的 toString 方法来判断输出的字符串。 因为最原始的 Object 的原型对象 prototype 的 toString 方法返回的字符串总为:[object ** ]  ,(其中 ** 可代表: Array  或 Function 或 Object 等 

    isNull:  {  return o === null ;}

    isUndefined : {  return typeof o === "undefined"   }

    isBoolean: { return typeof o == "boolean" }

    isNumber: {return typeof == "number" } //isFinite 来检测数字是否为有限的,即不是 NaN、负无穷或正无穷

    isArray: { return ( Object.prototype.toString.call(o) =="[object Array]" ) }

    isString: {return typeof o == "string"; }

    isFunction: {return typeof o === "function" || ( Object.prototype.toString.call(o) =="[object function]" ) }

    isObject:{return o&&( typeof o =="object"||isFunction(o) ) }  //认为Object 与 Function 都是 Object。

  • 相关阅读:
    shell脚本while read line的使用
    shell 一次性赋值多个变量
    Docker Volume
    nginx反向代理与负载均衡
    lua中 table.getn(t) 、#t、 table.maxn(t) 这三个什么区别?
    pv绑定pvc
    使用brew services管理服务
    Kubernetes中强制删除Pod、namespace
    shell 中的${},##, %% , :- ,:+, ? 的使用
    petalinux中fsbl源码
  • 原文地址:https://www.cnblogs.com/mengxiang-1234/p/4688166.html
Copyright © 2011-2022 走看看