在開始之前先了解下js數(shù)據(jù)類型
null undefined number boolean?string
function object array
typeof是用來判斷數(shù)據(jù)類型的,就一個參數(shù) ,使用方式像這樣:?typeof ?num, ?就是判斷num是什么類型
typeof 一般只能返回如下幾個結(jié)果:"number"、"string"、"boolean"、"object"、"function" 和 "undefined"; 除了"object" 其他都好說。
著重看這幾個:
typeof 不存在的變量 = “undefined”
typeof 對象 = “object”
typeof ?null = "object"?
typeof 數(shù)組 = “object”
typeod 方法的實例(比如 new Array()) =“object”
對象,數(shù)組 都是引用類型, 使用typeof 結(jié)果是 object類型,但是null 是基本數(shù)據(jù)類型,使用typeof結(jié)果也是 object,
可以這么理解:null 是?不指向任何對象?的?空指針, 因為它是指向?qū)ο蟮?,所以typeof 就是 object, 但是它又是空的,所以就屬于基本數(shù)據(jù)類型。
但是要想判斷一個變量是不是數(shù)組, 或者對象, 這時候就需要instanceof了(判斷是不是null,直接用 ?變量 === null 就行, null===null 結(jié)果是 true)
1 語言規(guī)范中是如何定義這個運算符的。
2 JavaScript 原型繼承機(jī)制。
function instance_of(L, R) {//L 表示左表達(dá)式,R 表示右表達(dá)式 var O = R.prototype; L = L.__proto__; while (true) { if (L === null) return false; if (O === L) // 這里重點:當(dāng) O 嚴(yán)格等于 L 時,返回 true return true; L = L.__proto__; } }
?對上圖的說明:
1.function Foo 就是一個方法,比如內(nèi)置的 Array ,String ,或者自定義的方法;
2.function Object 就是Object
3.function Function 就是Function
? ? ? ?4.以上三個其實都是function 所以他們的_proto_ 都是Function.prototype
5.記住 String, Array, Number, Object, Function這些其實都是 function
function F(){}var a = new F()
console.log(a instanceof F) // 輸出 true
? function child(){}
? function father(){}
? child.prototype = new father()?
var a = new child()
? console.log(a instanceof father) // 輸出true
console.log(Object instanceof Object);//trueconsole.log(Function instanceof Function);//trueconsole.log(Number instanceof Number);//falseconsole.log(String instanceof String);//falseconsole.log(Function instanceof Object);//trueconsole.log(Foo instanceof Function);//trueconsole.log(Foo instanceof Foo);//false
大家可以在控制臺輸出,可以直觀的看到每個步驟的輸出,結(jié)合instanceof 的規(guī)范跟js原型鏈 加深理解。
對于用 typeof 就可以判斷出來數(shù)據(jù)類型的這里就不處理,只處理 typeof 結(jié)果為 object ,并且不是 null 的。
方法一: 直接使用instanceof的規(guī)則
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function instance_of(L, R) {//L 表示左表達(dá)式,R 表示右表達(dá)式
var O = R.prototype;
L = L.__proto__;
while (true) {
if (L === null)
return false;
if (O === L) // 這里重點:當(dāng) O 嚴(yán)格等于 L 時,返回 true
return true;
L = L.__proto__;
}
}
// 開始測試
var a = []
var b = {}
function Foo(){}
var c = new Foo()
function child(){}
function father(){}
child.prototype = new father()
var d = new child()
console.log(instance_of(a, Array)) // true
console.log(instance_of(b, Object)) // true
console.log(instance_of(b, Array)) // false
console.log(instance_of(a, Object)) // true
console.log(instance_of(c, Foo)) // true
console.log(instance_of(d, child)) // true
console.log(instance_of(d, father)) // true
</script>
</body>
</html>
?
方法二:在方法一的基礎(chǔ)上使用 constructor (此方法無法用于判斷繼承)
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function instance_of(L, R) {//L 表示左表達(dá)式,R 表示右表達(dá)式
var O = R;
L = L.__proto__;
while (true) {
if (L === null)
return false;
if (O === L.constructor) // 這里重點:當(dāng) O 嚴(yán)格等于 L 時,返回 true
return true;
L = L.__proto__;
}
}
// 開始測試
var a = []
var b = {}
function Foo(){}
var c = new Foo()
function child(){}
function father(){}
child.prototype = new father()
var d = new child()
console.log(instance_of(a, Array)) // true
console.log(instance_of(b, Object)) // true
console.log(instance_of(b, Array)) // false
console.log(instance_of(a, Object)) // true
console.log(instance_of(c, Foo)) // true
console.log(instance_of(d, child)) // false 這里就是無法用于判斷繼承的
console.log(instance_of(d, father)) // true
</script>
</body>
</html>
??
來源:http://www.icode9.com/content-4-148601.html