JavaScript 检测类型的四种方法

  1. Object.prototype.toString.call() 
  2. typeof
  3. instanceof
  4. constructor

​​​​​​​

1. Object.prototype.toString.call() 获取Object.portotype上的toString方法,让方法的this变为需要检测的数据类型值。该方法最准确!

let num = 123
let str = "string"
let bool = true

let arr = []
let obj = {}
let fn = function () { }

console.log(Object.prototype.toString.call(num));// [object Number]
console.log(Object.prototype.toString.call(str));// [object String]
console.log(Object.prototype.toString.call(bool));// [object Boolean]

console.log(Object.prototype.toString.call(arr));// [object Array]
console.log(Object.prototype.toString.call(obj));// [object Object]
console.log(Object.prototype.toString.call(fn));// [object Function]

console.log(Object.prototype.toString.call(null));// [object Null]
console.log(Object.prototype.toString.call(undefined));//[object Undefined]

2. typeof 可用于检测基本数据类型,null和引用数据类型都为 object,内置构造函数和函数都为 function。

console.log(typeof 123); // number
console.log(typeof 'str'); // string
console.log(typeof true); // boolean
console.log(typeof null); // object
console.log(typeof undefined); // undefined
console.log(typeof []); // object
console.log(typeof Array); // function
console.log(typeof {}); // object
console.log(typeof Object); // function
console.log(typeof aaaaa) // undefined,未定义的变量也为undefined

3. instanceof 用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上,返回布尔值。不能检测基本数据类型,基本数据类型都为false。

let str = "is a string"; 
let newString = new String();
let newStr = new String("is a string");
let arr = []
let obj = {}
let fn = function () { }
let cla = class { }

console.log(str instanceof String);// false, simpleStr 并不是对象
console.log(newString instanceof String);// true
console.log(newStr instanceof String);// true
console.log(newString instanceof Object);// true

console.log(arr instanceof Array); // true
console.log(arr instanceof Object); // true

console.log(obj instanceof Object); // true

console.log(fn instanceof Function); // true
console.log(fn instanceof Object); // true

console.log(cla instanceof Function); // true
console.log(cla instanceof Object); // true

4.constructor 返回构造函数

let num = 123
let str = "123"
let bool = true
let arr = []
let obj = {}
let fn = function () { }

console.log(num.constructor);  // ƒ Number() { [native code] }
console.log(str.constructor);  // ƒ String() { [native code] }
console.log(bool.constructor); // ƒ Boolean() { [native code]
console.log(arr.constructor);  // ƒ Array() { [native code] }
console.log(obj.constructor);  // ƒ Object() { [native code] }
console.log(fn.constructor);  // ƒ Function() { [native code] }

但是constructor并不可靠,可以被修改,即使被修改了,也不会影响代码的正常运行。

function FUNA() { }
FUNA.prototype = Array.prototype
let fnA = new FUNA()
console.log(fnA.constructor); // ƒ Array() { [native code] }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值