• typeof检测几种较为特殊的值与Boolean强制转换哪几种值为false Amy


    typeof是操作符,不是函数,可以用它来检测给定变量的数据类型,共有6个取值:"undefined"/"number"/"string"/"object"/"function"/"boolean"

    下面是几种较为特殊的值

    alert(typeof NaN);//number
    alert(typeof Number.MAX_VALUE);//number
    alert(typeof Number.POSITIVE_INFINITY);//number
    var a=function(){
    };
    alert(typeof a);//function
    alert(typeof function(){});//function
    alert(typeof {});//object
    alert(typeof []);//object
    alert(typeof null);//object
    //
    alert(typeof b);//undefined
    var c;
    alert(typeof c);//undefined
    //未初始化未定义typeof返回的均为undefined,但是它们还是有区别的,如果alert一个从未定义的变量alert(b)//出错
    //如果alert一个未初始化的变量alert(c)//undefined

     除了下面5个特殊值,其他任何类型的数据转换为逻辑值时都是true,NaN,null,undefined,0,""

    alert(Boolean(NaN));//false
    alert(Boolean(undefined));//false
    alert(Boolean(""));//false
    alert(Boolean(0));//false
    alert(Boolean(null));//false
  • 相关阅读:
    一个前端框架应该有的一些公共函数
    前端开发避免bug注意事项
    angular中iframe的ng-src属性
    jquery上传base64位图片
    [Luogu]P2302 loidc,跑起来
    情书
    SA学习笔记
    关于GCD的证明
    文本生成器(AC自动机 + DP)
    AC自动机--速成版
  • 原文地址:https://www.cnblogs.com/amy2011/p/3131636.html
Copyright © 2020-2023  润新知