【JS】还在用typeof判断对象吗?判断所有数据类型的方法,判断是否为对象

首先通用的判断方法需要完全掌握。
本文详细说明了对象,数组,函数的判断方法,可在相关场景下使用。
做算法题可能需要筛选有限数或NaN等特殊数字。

通用判断

1. 使用 typeof 判断,一般判断基本类型

var obj = {
   name: 'zhangxiang'
};

function foo() {
    console.log('this is a function');
}

var arr = [1,2,3];

console.log(typeof 1);  // number
console.log(typeof '1');  //string
console.log(typeof true);  //boolean
console.log(typeof null); //object
console.log(typeof undefined); //undefined
console.log(typeof obj); //object
console.log(typeof foo);  //function
console.log(typeof arr);   //object

在一些复杂的场景比如 object 与 null, array 与 object, function 与 object 等等的类型区分, typeof 就会显得心有余力不足了.

2. 使用 instanceof 判断有原型链的数据,通常判断引用类型,但是需要确保原型链未改变过

var obj = {}; obj instanceof Object; //=> true; 
var arr = []; arr instanceof Array; //=> true;
var fn = function() {}; fn instanceof Function; //=> true;
null instanceof Object // false

3. 使用 Object.prototype.toString 方法, 可以获取到变量的准确的类型.

function foo () {};
Object.prototype.toString.call(1);  '[object Number]'
Object.prototype.toString.call('1'); '[object String]'
Object.prototype.toString.call(NaN); '[object Number]'
Object.prototype.toString.call(foo);  '[object Function]'
Object.prototype.toString.call([1,2,3]); '[object Array]'
Object.prototype.toString.call(undefined); '[object Undefined]'
Object.prototype.toString.call(null); '[object Null]'
Object.prototype.toString.call(true); '[object Boolean]'

Object.prototype.toString 的原理是当调用的时候, 就取值内部的 [[Class]] 属性值, 然后拼接成 '[object ’ + [[Class]] + ‘]’ 这样的字符串并返回. 然后我们使用 call 方法来获取任何值的数据类型.

*Object.prototype.toString 方法看起来很好用,但是如果使用Object.prototype.toString.call(new Number(1))会得到[object Number],需要注意!

判断数字

1. 判断一个可能是undefinednull的值是不是NaN(只能筛查出为NaN的值,如果需要找出所有有限数不能直接!Number.isNaN())

  • Number.isNaN() // true
	// 一个仿isNaN方法
	function areYouNaN (val) {
	    if (val !== val && typeof val === 'number') {
	        return "yes!"
	    } else {return "No.I'm not!"}
	}
	areYouNaN(NaN)
	areYouNaN(0)
	areYouNaN(false)
	areYouNaN(undefined)
	areYouNaN(null)
  • 🉑 var s = new Set([NaN]);s.has(NaN) // true ----可利用set类型的判断规则,它使用的算法叫做“Same-value equality”,类似于精确相等运算符( ===),主要的区别是判断NaN等于自身,而精确相等运算符认为NaN不等于自身,与===的不同

2. 判断一个可能是undefinednull的值是不是普通数字

  • Number.isFinite()// true ----- 可以判断出一个值是否为非NaNInfinity-Infinity的有限数, 但是会有兼容性问题,不兼容IE
    在这里插入图片描述
    *使用Number.isFinite(val)和Number.isNaN(val)都不用额外判断val是否为Number值,非Number类型返回false
    3. 判断是否为数字类型
  • typeof() === 'number’// true
  • 🉑 Object.prototype.toString.call() === '[object Number]'// true

判断对象

1. 判断是否是对象

  • ❌(可以用typeof但没必要, 需要判断不是null和array)typeof obj === ‘object’ && obj !== null && Array.isArray(obj) === false — 狭义的对象,不为function、array或null
function ifO(obj){
  var type = typeof obj;
  return type === 'object' && obj !== null && Array.isArray(obj) === false
}
var ooo = {'1': '23'}
function a() {}
ifO(ooo) // true
ifO([]) // false
ifO(a) // false
  • Object.prototype.toString.call(obj) === '[object Object]'
  • var obj = {}; obj instanceof Object;

判断数组

1. 判断是否为数组

  • Array.isArray() // true ---- isArray 是数组类型内置的数据类型判断函数
  • 🉑 Object.prototype.toString.call(array) === ‘[object Array]’;
  • 🉑 var arr = []; arr instanceof Array;

判断函数

1. 判断是否为函数

  • typeof fun === 'function’
  • 🉑 Object.prototype.toString.call(fun) === ‘[object Function]’;
  • 🉑 fun instanceof Function;
function fun () {}; fun instanceof Function;
// true
function fun () {}; Object.prototype.toString.call(fun) === '[object Function]'
// true
function fun () {}; typeof fun === 'function'
// true

引用文章内容:
JavaScript 中如何判断变量是否为数字
Javascript 中的数据类型判断

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值