一、typeof判斷資料型別(判斷陣列跟物件都返回object)
console.
log(
typeof
null);
//object
console.
log(
typeof
function()
);//function
console.
log(
typeof
'小a');
//string
console.
log(
typeof1)
;//number
console.
log(
typeof a)
;//undefined
console.
log(
typeof undefined)
;//undefined
console.
log(
typeof
);//object
console.
log(
typeof
nan)
;//number
console.
log(
typeof);
//object
二、instanceof判斷物件的原型鏈是否是指向建構函式的prototype
var arr =[1
,2,3
,1];
console.
log(arr instanceof
array);
//true
=== array); //true
三、物件的constructor屬性
var arr =[1
,2,3
,1];
console.
log(arr.constructor === array)
;//true
四、object.prototype.tostring.call(arr)
利用物件的tostring可以準確判斷是什麼型別,call()改變this指向。
console.
log(object.prototype.tostring.
call
("jerry"))
;//[object string]
console.
log(object.prototype.tostring.
call(12
));//[object number]
console.
log(object.prototype.tostring.
call
(true))
;//[object boolean]
console.
log(object.prototype.tostring.
call
(undefined));
//[object undefined]
console.
log(object.prototype.tostring.
call
(null))
;//[object null]
console.
log(object.prototype.tostring.
call()
);//[object object]
console.
log(object.prototype.tostring.
call
(function()
));//[object function]
console.
log(object.prototype.tostring.
call([
]));
//[object array]
console.
log(object.prototype.tostring.
call
(new
date))
;//[object date]
console.
log(object.prototype.tostring.
call
(/\d/))
;//[object regexp]
console.
log(object.prototype.tostring.
call
(new
person))
;//[object object]
五、es6方法 - array.isarray()
array.isarray(); //true
判斷乙個物件是否為陣列
方法一 使用instanceof操作符。instanceof操作符用來判斷要檢測物件的原型鏈上是否存在某個建構函式的prototype屬性。var a var b console.log a instanceof object true console.log b instanceof object...
判斷乙個物件是否是陣列
用typeof方法返回的陣列型別是object,無法確定是否是陣列,但可以通過下面方法判斷。var is array function value 說明 首先判斷這個值是否有值,並且不是undifined或null的值 其次判斷這個值的typeof運算的結果是否是object 第三判斷這個值的len...
判斷乙個物件是否是陣列?
一 判斷乙個物件是否是陣列?var obj1 obj2 1 2 obj3 newdate 1.判斷當前物件的原型物件是否是陣列的原型物件 1.使用 proto 獲得物件的原型物件 obj1.proto array.prototype false obj2.proto array.prototype ...