两只小蚂蚁

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

类型

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
posted on 2018-09-01 13:01  两只小蚂蚁  阅读(143)  评论(0编辑  收藏  举报