返回乙個字串,用來判斷資料所屬的基本型別(null除外),但引用型別的判斷結果都為object(function除外)
console.
log(
typeof1)
;//number
console.
log(
typeof
'1,2');
//string
console.
log(
typeof
false);
//boolean
console.
log(
typeof undefined)
;//undefined
console.
log(
typeof
null);
//object
console.
log(
typeof[1
,2])
;//object
console.
log(
typeof);
//object
console.
log(
typeof
function()
);//function
console.
log(
typeof
newdate()
);//object
也就是說當使用instanceof進行型別判斷時,會在fn的原型鏈中向上查詢,直到找到建構函式.prototype,如果找到了,就會返回true,否則返回falsevar
fun=
function()
;var fn =
newfun()
;console.
log(fn instanceof
fun)
;//true
console.
log(fn instanceof
object);
//true
console.
log(undefined instanceof
undefined);
//undefined is not defined
console.
log(
null
instanceof
null);
//null is not defined
console.
log(
1instanceof
number);
//false
console.
log(
'1'instanceof
string);
//false
console.
log(
true
instanceof
boolean);
//false
console.
log([1
]instanceof
array);
//true
console.
log(
function()
instanceof
function);
//true
console.
log(
instanceof
object);
//true
str是js包裝過的,相當於js內部執行了str = new string(『i am string』),因此,此時的str的建構函式為string,判斷結果為true。var str=
'i am string'
;//原始資料型別
console.
log(str.constructor == string)
;//true
var unde = undefined
var nul =
null
var num =
1console.
log(unde.constructor == undefined)
;//cannot read property 'constructor' of undefined
console.
log(nul.constructor == null)
;//cannot read property 'constructor' of null
console.
log(num.constructor == number)
;//true
console.
log(
'1'.constructor == string)
;//true
console.
log(
true
.constructor == boolean)
;//true
console.
log([1
].constructor == array)
;//true
console.
log(
function()
.constructor == function)
;//true
console.
log(
.constructor == object)
;//true
因此,直接通過object.prototype呼叫這個方法,就可以準確的判斷出所屬型別。console.
log(
'1'.
tostring()
);//'1'
console.
log([1
].tostring()
);//'1'
delete array.prototype.tostring;
//刪掉array繼承的tostring方法,就會去建構函式,也就是object中去找
console.
log([1
].tostring()
);//[object array]
判斷是否為陣列console.
log(object.prototype.tostring.
call
('1'))
;// [object string]
console.
log(object.prototype.tostring.
call(1
);// [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
(function()
);// [object function]
console.
log(object.prototype.tostring.
call
(new
date()
);// [object date]
console.
log(object.prototype.tostring.
call([
1]);
// [object array]
console.
log(object.prototype.tostring.
call
(new
regexp()
);// [object regexp]
判斷是否為數值console.
log(array.
isarray([
1,2]
));//true
console.
log(array.
isarray
('1,2'))
;//false
console.
log(
isnan([
1,2]
));//true,即非數值
console.
log(
isnan
('1,2'))
;//true,即非數值
console.
log(
isnan(12
));//false,即為數值
js中判斷資料型別的幾種方法
1.typeof typeof data 返回data的型別字串形式。如 typeof a string typeof返回的值 1 undefined 如果這個值未定義 2 boolean 如果這個值是布林值 3 string 如果這個值是字串 4 number 如果這個值是數字 5 functio...
js判斷資料型別幾種方法
js資料型別的判斷主要有四種方法 typeof instanceof constructor object.prototype.tostring.call 資料型別的包括 number boolean string symbol object array undefined null functio...
js判斷資料型別的幾種方法
判斷js中的資料型別有一下幾種方法 typeof instanceof constructor prototype type jquery.type 接下來主要比較一下這幾種方法的異同。先舉幾個例子 var a iamstring.var b 222 var c 1,2,3 var d new da...