zoukankan      html  css  js  c++  java
  • JavaScript 类型判断

    类型

    1. 基础类型种类

    JavaScript中的5种基本类型:

    • Undefined
    • Null
    • Boolean
    • Number
    • String

    2. 引用类型

    定义: 引用类型是指可能由多个值构成的对象
    JavaScript中对象都是引用类型,而Function是一种特殊的对象(也是引用类型)
    引用类型形成方式:

    • new关键字,包括对基本类型使用new关键字
    • {}对象定义,如var obj={}
    • 函数定义,如function func(){}
    • 函数赋值,如var funb = func;

    3. typeof返回值

    类型 返回值
    Undefined "undefined"
    Null "object"
    Boolean "boolean"
    Number "number"
    String "string"
    Symbol "symbol"
    函数对象 "function"
    任何其他对象 "object"

    4. 类型判断

    • typeof
    • a instanceof 类型
    • a.constructor === 类型
    • Object.prototype.toString.call(a)
    定义 typeof prototype
    var a = undefined; typeof(a); //"undefined" Object.prototype.toString.call(a); //"[object Undefined]"
    var b = null; typeof(b); //"object" Object.prototype.toString.call(b); //"[object Null]"
    var c = true; typeof(c); //"boolean" Object.prototype.toString.call(c); //"[object Boolean]"
    var d = 1; typeof(d); //"number" Object.prototype.toString.call(d); //"[object Number]"
    var e = "1"; typeof(e); //"string" Object.prototype.toString.call(e); //"[object String]"
    var f = function funF(){}; typeof(f); //"function" Object.prototype.toString.call(f); //"[object Function]"
    var g = new Boolean(); typeof(g); //"object" Object.prototype.toString.call(g); //"[object Boolean]"
    var h = new Number(); typeof(h); //"object" Object.prototype.toString.call(h); //"[object Number]"
    var i = new String(); typeof(i); //"object" Object.prototype.toString.call(i); //"[object String]"
    var j = {}; typeof(j); //"object" Object.prototype.toString.call(j); //"[object Object]"
    var k = []; typeof(k); //"object" Object.prototype.toString.call(k); //"[object Array]"
    var l = new Date(); typeof(l); //"object" Object.prototype.toString.call(l); //"[object Date]"

    5. 原理

    • typeof
      在早期JavaScript中,数据的值存储在一个32bit的单元中,其中的低位包括一个(1-3bit)的标记位,剩下的才是实际值。
      当在JS中使用typeof判断类型的时候,就是判断的这个标记位。
      其中有个好玩的是null的类型也是以000开头,所以有 typeof null == "object",详细情况网上很多文章介绍有。

    类型定义的源码可以参照下面:

    //具体可查看:https://dxr.mozilla.org/classic/source/js/src/jsapi.h
    #define JSVAL_OBJECT            0x0     /* untagged reference to object */
    #define JSVAL_INT               0x1     /* tagged 31-bit integer value */
    #define JSVAL_DOUBLE            0x2     /* tagged reference to double */
    #define JSVAL_STRING            0x4     /* tagged reference to string */
    #define JSVAL_BOOLEAN           0x6     /* tagged boolean value */
    
    • instanceof
      A instanceof B: 遍历A的原型链prototype,如果存在有A.*.prototype等于B则返回true,如果查找到顶层Object都没有则返回false

    6. 源码解析

    基础类型可以参考下面的连接:
    https://dxr.mozilla.org/classic/source/js/src/jsapi.h

    至于V8引擎中的引用类型和类型继承如下:
    已做删减,只留下了常见部分
    源文件地址: https://github.com/v8/v8/blob/master/src/objects.h

    // Inheritance hierarchy:
    // - Object
    //   - Smi          (immediate small integer)
    //   - HeapObject   (superclass for everything allocated in the heap)
    //     - JSReceiver  (suitable for property access)
    //       - JSObject
    //         - JSArray
    //         - JSArrayBuffer
    //         - JSArrayBufferView
    //         - JSCollection
    //           - JSSet
    //           - JSMap
    //         - JSStringIterator
    //         - JSSetIterator
    //         - JSMapIterator
    //         - JSWeakCollection
    //           - JSWeakMap
    //           - JSWeakSet
    //         - JSRegExp
    //         - JSFunction
    //         - JSGeneratorObject
    //         - JSGlobalObject
    //         - JSGlobalProxy
    //         - JSValue
    //           - JSDate
    //         - JSMessageObject
    //       - JSProxy
    //     - FixedArrayBase
    //       - ByteArray
    //       - BytecodeArray
    //       - FixedArray
    //         - DescriptorArray
    //         - FrameArray
    //         - HashTable
    //           - Dictionary
    //           - StringTable
    //           - StringSet
    //           - CompilationCacheTable
    //           - MapCache
    //         - OrderedHashTable
    //           - OrderedHashSet
    //           - OrderedHashMap
    //         - Context
    //       - FixedDoubleArray
    //     - Name
    //       - String
    //       - Symbol
    //     - HeapNumber
    //     - BigInt
    //     - Cell
    //     - Code
    //     - Map
    
  • 相关阅读:
    Android Studio 单刷《第一行代码》系列 05 —— Fragment 基础
    Android Studio 单刷《第一行代码》系列 04 —— Activity 相关
    Android Studio 单刷《第一行代码》系列 03 —— Activity 基础
    Android Studio 单刷《第一行代码》系列 02 —— 日志工具 LogCat
    Android Studio 单刷《第一行代码》系列 01 —— 第一战 HelloWorld
    IDEA 内网手动添加oracle,mysql等数据源,以及server returns invalid timezone错误配置
    eclipse maven设置
    IntelliJ IDE 常用配置
    eclipse maven 常见问题解决方案
    Maven 安装和配置
  • 原文地址:https://www.cnblogs.com/full-stack-engineer/p/9569981.html
Copyright © 2011-2022 走看看