1.數(shù)據(jù)類型
ECMAScript規(guī)范定義了7種數(shù)據(jù)類型
,分為基本類型和引用類型
基本類型:String,Boolean,Number,Undefined,Null, Symbol
引用類型:Object
基本類型:保存在棧內(nèi)存中的簡(jiǎn)單數(shù)據(jù)段,按值訪問,操作的是他們實(shí)際保存的值。
引用類型:保存在堆內(nèi)存中的對(duì)象,意思是,變量中保存的實(shí)際上只是一個(gè)指針,這個(gè)指針指向內(nèi)存堆中實(shí)際的值;按引用訪問,當(dāng)查詢時(shí),我們需要先從棧中讀取內(nèi)存地址,接著根據(jù)地址保存在堆內(nèi)存中的值。
2. 判斷JS數(shù)據(jù)類型的四種方法
(1)typeof
- typeof 操作符返回這個(gè)表達(dá)式的數(shù)據(jù)類型(全小寫字母)
"string"—字符串
"boolean" —布爾值
"number"—數(shù)值
"undefined"—未定義或未初始化的變量
"symbol"— symbol值
"object"—對(duì)象或者null
"function"—函數(shù)
typeof " "; // string
typeof true; //boolean
typeof 1000; // number
typeof Symbol(); // symbol
typeof undefined; //undefined
typeof new Function(); // function
typeof null; //object
typeof [] ; //object
typeof new Date(); //object
總結(jié)一下規(guī)則
基本類型,除 null 以外,均可以返回正確的結(jié)果
引用類型,除 function 返回 function 類型以外,一律返回 object 類型
對(duì)于 null ,返回 object 類型
(2) instanceof
- instanceof 用來判斷 A 是否為 B 的實(shí)例,表達(dá)式為:A instanceof B。特別注意的是:instanceof 檢測(cè)的是原型,即 A的 proto 是否指向 B 的原型對(duì)象
{} instanceof Object;// true
[] instanceof Array; // true
[] instanceof Object; // true
function Person(){};
new Person() instanceof Person; //true
new Person instanceof Object;// true
instanceof 只能判斷兩個(gè)對(duì)象是否屬于實(shí)例關(guān)系, 而不能判斷一個(gè)對(duì)象實(shí)例具體屬于哪種類型
,這也是我們很少使用instanceof 判斷數(shù)據(jù)類型的原因。
instanceof 操作符的另外一個(gè)問題是:它假定只有一個(gè)全局執(zhí)行環(huán)境。如果網(wǎng)頁中包含多個(gè)框架,那實(shí)際上就存在兩個(gè)以上不同的全局執(zhí)行環(huán)境,從而存在兩個(gè)以上不同版本的構(gòu)造函數(shù)。如果你從一個(gè)框架向另一個(gè)框架傳入一個(gè)數(shù)組,那么傳入的數(shù)組與在第二個(gè)框架中原生創(chuàng)建的數(shù)組分別具有各自不同的構(gòu)造函數(shù)。
(3) constructor
-
constructor 判斷構(gòu)造函數(shù)的方式
當(dāng)一個(gè)函數(shù) F被定義時(shí),JS引擎會(huì)為F添加 prototype 原型,然后再在 prototype上添加一個(gè) constructor 屬性,并讓其指向 F 的引用。
function F(){
}
var f=new F();
f.constructor ===F //true
//注意: constructor 在類繼承時(shí)會(huì)出錯(cuò)
function A(){};
function B(){};
A.prototype = new B(); //A繼承自B
var a = new A();
a.constructor === B // true;
a.constructor === A //false;
//其實(shí)我們可以修復(fù)constructor指向
//而instanceof方法不會(huì)出現(xiàn)該問題,對(duì)象直接繼承和間接繼承的都會(huì)報(bào)true:
a instanceof B //true;
a instanceof A //true;
細(xì)節(jié)問題:
null 和 undefined 是無效的對(duì)象,因此是不會(huì)有 constructor 存在的,這兩種類型的數(shù)據(jù)需要通過其他方式來判斷。
函數(shù)的 constructor 是不穩(wěn)定的,這個(gè)主要體現(xiàn)在自定義對(duì)象上,當(dāng)開發(fā)者重寫 prototype 后,原有的 constructor 引用會(huì)丟失,constructor 會(huì)默認(rèn)為 Object
(4)Object.prototype.toString.call()
- Obeject原型上的toString方法,該方法獲取this對(duì)象上的[[Class]]屬性,這個(gè)屬性會(huì)返回該對(duì)象的類型,其格式為 [object Xxx] ,其中 Xxx 就是對(duì)象的類型
對(duì)于 Object 對(duì)象,直接調(diào)用 toString() 就能返回 [object Object] 。而對(duì)于其他對(duì)象,則需要通過 call / apply 來調(diào)用才能返回正確的類型信息。
Object.prototype.toString.call(new Function()) ; // [object Function]
Object.prototype.toString.call(new Date()) ; // [object Date]
Object.prototype.toString.call([]) ; // [object Array]
Object.prototype.toString.call(new RegExp()) ; // [object RegExp]
Object.prototype.toString.call(new Error()) ; // [object Error]
Object.prototype.toString.call(document) ; // [object HTMLDocument]
Object.prototype.toString.call(window) ; //[object global] window 是全局對(duì)象 global 的引用
(5)使用特性判斷
總結(jié)
通常我們只需要使用typeof + toString方法就可以識(shí)別全部數(shù)據(jù)類型