JavaScript中的5種基本類型:javascript
定義: 引用類型是指可能由多個值構成的對象
JavaScript中對象都是引用類型,而Function是一種特殊的對象(也是引用類型)
引用類型造成方式:java
類型 | 返回值 |
---|---|
Undefined | "undefined" |
Null | "object" |
Boolean | "boolean" |
Number | "number" |
String | "string" |
Symbol | "symbol" |
函數對象 | "function" |
任何其餘對象 | "object" |
定義 | 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]" |
類型定義的源碼能夠參照下面:git
//具體可查看: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 */
基礎類型能夠參考下面的鏈接:
https://dxr.mozilla.org/classic/source/js/src/jsapi.hgithub
至於V8引擎中的引用類型和類型繼承以下:
已作刪減,只留下了常見部分
源文件地址: https://github.com/v8/v8/blob/master/src/objects.hapi
// 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