• typeof详解


    typeof operator 返回了表示对象类型的字符串

    下表列出了typeof可能的返回值。

    TypeResult
    Undefined "undefined"
    Null "object" 
    Boolean "boolean"
    Number "number"
    String "string"
    Symbol (ECMAScript 2015的新特性) "symbol"
    Host object (js环境提供) 依赖于具体的浏览器环境
    Function object (implements [[Call]] in ECMA-262 terms) "function"
    Any other object "object"

    举例:

    // Numbers

    typeof 37 === 'number';

    typeof 3.14 === 'number';

    typeof Math.LN2 === 'number';

    typeof Infinity === 'number';

    typeof NaN === 'number'; // 虽然NAN的意思是“不是一个数字” "Not-A-Number"

    typeof Number(1) === 'number'; // but never use this form!

    // Strings

    typeof "" === 'string';

    typeof "bla" === 'string';

    typeof (typeof 1) === 'string'; // typeof永远返回string

    typeof String("abc") === 'string'; // but never use this form!

    // Booleans

    typeof true === 'boolean';

    typeof false === 'boolean';

    typeof Boolean(true) === 'boolean'; // but never use this form!

    // Symbols

    typeof Symbol() === 'symbol'

    typeof Symbol('foo') === 'symbol'

    typeof Symbol.iterator === 'symbol'

    // Undefined

    typeof undefined === 'undefined';

    typeof 声明了但是没有定义的变量=== 'undefined';

    typeof没有声明的变量 === 'undefined';

    // Objects

    typeof {a:1} === 'object';
    //使用Array.isArray或者Object.prototypr.toString.call来判断普通对象和数组

    typeof [1, 2, 4] === 'object';

    typeof new Date() === 'object';

    //注意不要用下面的判断!

    typeof new Boolean(true) === 'object';

    typeof new Number(1) === 'object';

    typeof new String("abc") === 'object';

    // Functions

    typeof function(){} === 'function';

    typeof class C {} === 'function';

    typeof Math.sin === 'function';

    null

    //从js实现之初就存在 typeof null === 'object';

    在最初JS的实现时,js值包括了一个值和一个类型标签(a type tag)。对象的类型标签是0。null是空指针,在大多数平台指向0x00。所以,null的类型标签也是0.因此,type null返回object。

    Regular expressions

    可调用的正则表达式(Callable regular expressions)在一些浏览器里不统一。

    typeof /s/ === 'function'; // Chrome 1-12 和ECMAScript 5.1标准不一致

    typeof /s/ === 'object'; // Firefox 5+ 符合ECMAScript 5.1标准

     

    ie特性

    在IE6-8中许多许多宿主对象typeof之后返回的是object而不是function 。例如:

    typeof alert === 'object'

  • 相关阅读:
    排序——插入排序
    利用socket传文件
    Segmentation fault (core dumped)
    Linux网络编程
    3G功能设计及实现
    rpm命令
    安装包相互依赖的问题
    centos网站(一些软件的下载)
    解决vim显示中文的问题
    glGetFloatv (GL_MODELVIEW_MATRIX, mat)
  • 原文地址:https://www.cnblogs.com/RachelChen/p/5416646.html
Copyright © 2020-2023  润新知