typeof、instanceof、constructor、toString四种检测数据类型的方法;优缺和区别分析

typeof
instanceof
constructor
Object.prototype.toString.call
使用简单能检测出引用类型
基本能检测所有的类型(除了null和undefined)
检测出所有的类型
只能检测出基本类型
(除了null)
不能检测出基本类型,且不能跨iframe
constructor 易被修改,也不能跨 iframe
IE6 下, undefined null 均为Object

基本数据类型:

        字符串(String)、数字(Number)、布尔(Boolean)、空(Null)、未定义(Undefined

引用数据类型:

        对象(Object)、数组(Array)、函数(Function)。

1.typeof

 console.log( typeof 'hello world' )   // 返回 string
 console.log( typeof 100 )             // 返回 number
 console.log( typeof true)             // 返回 boolean

 let now = new Date();
 console.log( typeof now)             // 返回 object

 let arr = ['a',1,2,15,'zhangsan']
 console.log( typeof arr)             // 返回 object

 let obj = {name:'ZhangSan', age:25}
 console.log( typeof obj)             // 返回 object

 function fun() {
     return '这是个方法'
 }
 console.log( typeof fun)       // 返回 object

 let person_1 = null;
 console.log( typeof person_1 ) // 返回 object
 
 let person_2 = undefined;
 console.log( typeof person_2 ) // 返回 undefined

           在 JavaScript 中, undefined 是一个没有设置值的变量。typeof 一个没有值的变量会返回 undefinednull 表示 "什么都没有"。null是一个只有一个值的特殊类型。表示一个空对象引用。

 2.instanceof

let arr = ['a',1,2,15,'zhangsan']
let obj = {name:'ZhangSan', age:25}
let now = new Date();
function fun() {}
let str ='hello world'
let num =123456

console.log(arr instanceof Array );    //true
console.log(obj instanceof Object );   //true
console.log(now instanceof Date ); //true
console.log(fun instanceof Function );      //true
console.log(num instanceof Number);//无法判断基本数据类型,会返回false
console.log(str instanceof String);//无法判断基本数据类型,会返回false

 3.constructor

//数字
var num =12345
console.log(num.constructor) //ƒ Number() { [native code] }
//布尔值
console.log(true.constructor) //ƒ Boolean() { [native code] }
//字符串
var str = 'hello world'
console.log(str.constructor) // ƒ String() { [native code] }
//函数
function fun() {}
console.log(fun.constructor) // ƒ Function() { [native code] }
//数组
var arr=[1,2,3,4]
console.log(arr.constructor) // ƒ Array() { [native code] }
//对象
var obj = {name:'ZhangSan', age:25}
console.log(obj.constructor) // ƒ Object() { [native code] }

 constructor 无法检测nullundefined,会报错的 。

4.toString

//判断基本数据类型
console.log(Object.prototype.toString.call(123456))       // [object Number]   --数字
console.log(Object.prototype.toString.call('hello world'))// [object String]   --字符串
console.log(Object.prototype.toString.call(null))         // [object Null]     --Null
console.log(Object.prototype.toString.call(true))         // [object Boolean]  --布尔型
console.log(Object.prototype.toString.call(undefined))    // [object Undefined]--Undefined

//判断原生引用类型(部分):

function fun() {}
console.log(Object.prototype.toString.call(fun))     // [object Function] --函数

var now = new Date();
console.log(Object.prototype.toString.call(now))     // [object Function] --日期

var arr = [1,2,3,4,5];
console.log(Object.prototype.toString.call(arr))     // [object Array] --数组

var obj = {name:'ZhangSan', age:25}
console.log(Object.prototype.toString.call(obj))     // [object Object] --对象

var reg = /^1[3456789]\d{9}$/
console.log(Object.prototype.toString.call(reg))     // [object RegExp] --正则表达式

  • 12
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天命爱心职责~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值