zoukankan      html  css  js  c++  java
  • 你不知道的 JavaScript 系列中( 1 ) - 内置类型

    JavaScript 有 7 中内置类型:
    空置 ( null )
    未定义( undefined )
    布尔值( boolean )
    数字( number )
    字符串( string )
    对象( object )
    符号( symbol, ES6 新增)

    除对象之外,其他统称为 “基本类型”

    typeof 运算符查看值的类型
    typeof undefined === 'undefined' // true
    typeof true === 'boolean' // true
    typeof 42 === 'number' // true
    typeof '42' === 'string' // true
    typeof { a: 1 } === 'object' // true
    typeof Symbol() === 'symbol' // true ES6 中新加入的类型

    特殊的情况

    null

    你可能注意到 null 类型不在此列。它比较特殊,typeof 对它对处理有问题
    typeof null === 'object'

    正确对结果应该是 'null',但这个 bug 由来已久,在 JavaScript 中已经存在了将近二十年,也许永远也不会修复,因为这牵扯到太对的 Web 系统,修复它会产生更多的 bug,令许多系统无法正常工作。null 是基本类型中唯一的一个假值

    function

    typeof function a(){} === 'function'; // true

    看起来 function 也是一个内置类型,但是实际上是 object 的一个子类型,函数不仅是对象,还可以拥有属性

    function a(b,c){}
    a.length // 2
    因为函数声明了两个命名参数,b和c,所以其 length 值为 2


    数组

    typeof [1,2,3] === 'object'; // true

    数组也是对象,也是 object 的一个子类型

  • 相关阅读:
    input填入字符会出现黄色
    安装Scrapy时出现问题scrapy unicodedecodeerror ascii codec cant decode byte 0xd1 in position
    SVN 出现:Previous operation has not finished; run 'cleanup' if it was interrupted。
    Myeclipse小技巧
    好的开发网站
    BZOJ 1968
    BZOJ 1010
    BZOJ 1015
    BZOJ 3875
    BZOJ 2705
  • 原文地址:https://www.cnblogs.com/wzndkj/p/12791620.html
Copyright © 2011-2022 走看看