• JavaScript类型判断


    几种方法:typeof,instanceof,Object.prototype.toString,constructor,duck type

    ES6引入了一种新的原始数据类型Symbol,表示独一无二的值。它是JavaScript语言的第七种数据类型,前六种是:Undefined、Null、布尔值(Boolean)、字符串(String)、数值(Number)、对象(Object)。

    JavaScript5种基本类型:undefined,Number,String,null,boolean

    js是一种宽松类型语言;


    特殊的数值:Infinity、NaN(与自己都不相等)、Number.MAX_VALUE、Number.POSITIVE_INFINITY、

    typeof返回值:

    function,object,boolean,string,number,undefined;

    对null使用typeof返回的是"object" 

     

    数据类型

    转化成true的值

    转化成false的值

    Boolean          

    ture

    false

    String    

    所有的非空字符串                

    ""(空字符串)

    Number

    任何非零数字(包括无穷大)            

    0和NaN                                    

    Object

    任何对象

    不存在

    Undefined          

    不存在

    undefined

     确定对象类型:typeof 不是object就是类型,否则tostring()方法返回值,但这样还是不能判断自定义类;

    1)typeof:

    function isUndefined(value){return typeof value == 'undefined';}

    function isDefined(value){return typeof value != 'undefined';}

    function isObject(value){return value != null && typeof value == 'object';}

    function isString(value){return typeof value == 'string';}

    function isNumber(value){return typeof value == 'number';}

    function isFunction(value){return typeof value == 'function';}

    function isBoolean(value) {

      return typeof value == 'boolean';

    }

    2)toString:

    function isDate(value){

      return toString.apply(value) == '[object Date]';

    }

    function isArray(value) {

      return toString.apply(value) == '[object Array]';

    }

    function isFile(obj) {

      return toString.apply(obj) === '[object File]';

    }

    3)(duck type)判断是否有某属性或方法:

    function isWindow(obj) {

      return obj && obj.document && obj.location && obj.alert && obj.setInterval;

    }

    function isScope(obj) {

      return obj && obj.$evalAsync && obj.$watch;

    }

    function isElement(node) {

      return node &&

        (node.nodeName  // we are a direct element

        || (node.bind && node.find));  // we have a bind and find method part of jQuery API

    }

    instanceof  

  • 相关阅读:
    改了一下分辨率,Areo特效奇迹般的恢复了...
    此连接需要活动的Internet连接
    Apple Mac OS X每日一技巧026:Spotlight打开文件所在的文件夹
    WP7有约(八):在ListPicker控件的选择页面上播放铃声
    WP7有约(七):实现铃声设置的播放图标的效果
    WP7有约(五):回到主页
    WP7有约:一个应用的破蛋过程
    WP7有约(六):AppBarUtils使用指南
    IE与firefox事件处理
    C#试题
  • 原文地址:https://www.cnblogs.com/zqiong/p/6181009.html
Copyright © 2020-2023  润新知