目录
Obeject.prototype.toString.call
判断数据类型有几种方法?
typeof
使用typeof可以准确检测出number、string、boolean、undefined、function,
使用typeof检测null、数组Array、对象Object都只能检测出Object,而不能进一步判断它们的类型。
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//判断数据类型方法一:typeof
console.log(typeof 123);
console.log(typeof "123");
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof function fun(){});
console.log(typeof null);
console.log(typeof [1,2,3]);
console.log(typeof {name:"ailin"});
</script>
</body>
</html>
效果
instanceof
需要使用new来声明构造,instanceof不能区分null和undefined,对于基本数据类型如果不是用new来构造的也检测不出来,但是如果是用new来声明的数据类型,就可以检测出数据类型,还能检测出多层数据关系。
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//判断数据类型方法2:instanceof
let num = new Number(123);
console.log(num instanceof Number);//true
let str = new String("abc");
console.log(str instanceof String);//true
let boo = new Boolean(true);
console.log(boo instanceof Boolean);//true
// let und = new undefined;//报错因为Undefined没有constructor属性,不能被new
// console.log(und instanceof undefined);
// let nul = new null;//报错因为null没有constructor属性不能被new
// console.log(nul instanceof null);
var arr = new Array();
console.log(arr instanceof Array);//true
let fun = new function(){}
console.log(fun instanceof function(){});//false
var obj = new Object;
console.log(obj instanceof Object);//true
</script>
</body>
</html>
效果
constructor
constructor属性返回所以JavaScript变量的构造函数,null和undefined没有constructor属性。
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//判断数据类型方法3:constructor属性
console.log((123).constructor===Number);//true
console.log("abc".constructor===String);//true
console.log(true.constructor===Boolean);//true
console.log([1,2,3].constructor===Array);//true
console.log(function(){}.constructor===Function);//true
console.log({name:"ailin",age:20}.constructor===Object);//true
console.log(new Date().constructor===Date);//true
</script>
</body>
</html>
效果
constructor不能判断null和undefined,并且使用constructor是不安全的,因为constructor的指向可以改变,例子如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<script>
function Person(){}
Person.prototype={
name:"ailin",
age:20,
say(){
console.log("hi");
}
}
var Teacher = new Person();
console.log(Teacher instanceof Person);//true
console.log(Teacher.constructor==Person);//false
</script>
<body>
</body>
</html>
instanceof表示Teacher仍然是Person的实例,但是constructor却表明Teache的构造函数(父类)不再是Person了,constructor与instanceof的结果不一致这就造成了instanceof的失真。
constructor 属性指向了Object,也就是说通过这种对象字面量方式改变原型对象之后,原型/实例对象的constructor属性指向了Object,它们的构造函数(父类)变成了Object。//constructor的指向改变
Obeject.prototype.toString.call
使用这种方法是最可靠的
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//判断数据类型方法4:Object.prototype.toString.call
let num=1;
console.log(Object.prototype.toString.call(num));
let str="abc";
console.log(Object.prototype.toString.call(str));
let boo=true;
console.log(Object.prototype.toString.call(boo));
let und=undefined;
console.log(Object.prototype.toString.call(und));
var fun=function(){}
console.log(Object.prototype.toString.call(fun));
var arr=Array();
console.log(Object.prototype.toString.call(arr));
var obj =Object();
console.log(Object.prototype.toString.call(obj));
</script>
</body>
</html>
效果
编写一个函数判断所有数据类型
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//编写一个函数判断所有数据类型
function _typeof(item){
let res=Object.prototype.toString.call(item);
res = res.split(" ")[1];
res=res.substr(0,res.length-1);
return res;
}
console.log(_typeof(123));
console.log(_typeof("abc"));
console.log(_typeof(true));
console.log(_typeof(null));
console.log(_typeof(undefined));
var fun=function(){}
console.log(_typeof(fun));
var arr=[1,2,3]
console.log(_typeof(arr));
var obj ={
name:ailin,
age:20
}
console.log(_typeof(obj));
</script>
</body>
</html>
效果
欢迎点赞、关注、收藏!
进入前端复习阶段了,我会及时的把一些近年来面试会问的、常考的面试题整理下来,如果你也正在复习前端的话,欢迎关注我的前端复习专栏,实时更新~
祝大家都能拿到大厂offer!