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