2.js判断变量类型:typeof、instanceof、constructor、Object.prototype.toString.call()的区别

  1. typeof 判断 不能判断对象类型 typeof [] typeof {} typeof null // 都返回 'object'
  2. instanceof 判断谁是谁的实例 依据是  __proto__
  3. constructor 可以找到这个变量是通过谁构造出来的
  4. Object.prototype.toString.call()   不能细分谁是谁的实例
/**
 * 一:typeof
 *
 * 1.typeof 可以用来判断js的原始数据类型
 * 2.typeof 判断引用数据类的的时候 判断不准确
 *
 *  例如:
 *  console.log(typeof []); // object
 *  console.log(typeof function () {}); // function
 *  console.log(typeof new RegExp(/A/)); // object
 *
 */

/**
 * 二:Object.prototype.toString.call()
 *
 * 1.可以判断出来原始数据类型是具体的什么类型
 * 2.缺点就是不能判断自定义的构造函数类型
 *
 * 例如:
 * console.log(Object.prototype.toString.call([])); // [object Array]
 * console.log(Object.prototype.toString.call(function () {})); // [object Function]
 * console.log(Object.prototype.toString.call(new RegExp(/A/))); // [object RegExp]
 *
 * 判断不出来自定义的构造函数A
 * class A {}
 * let a = new A();
 * console.log(Object.prototype.toString.call(a)); [object Object]
 */

/**
 * 三:instanceof
 * 1.可以判断是不是某个类型的实例
 * 2.不能校验原始数据类型
 *
 *
 *  console.log([] instanceof Array); // true // 原理 [].__proto__ === Array.prototype
 *  console.log([] instanceof Object); // true   原理 [].__proto__.__proto__ === Object.prototype
 *
 *  原始数据类型判断不出来
 *  console.log('abc' instanceof String); // false
 */


/**
 * 四:constructor
 *  判断 A的constructor是不是B构造出来的
 *
 */

定义一组变量

let num = 123;
let str = 'abcd';
let bool = true;
let obj = { a: '1' };
let arr = [1, 2, 3];
let fun = function () {};
let und = undefined;
let nul = null;
let date = new Date();
let reg = new RegExp();
let err = new Error();

1.使用typeof检测

// 简单数据类型 如数字、字符串、布尔、undefined、函数可以用typeof检测出来
// 复杂数据类型对象、数组、日期、正则、null 得到的都是'object'
console.log(
  typeof num, // number
  typeof str, // string
  typeof bool, // boolean
  typeof obj, // object
  typeof arr, // object
  typeof nul, // object
  typeof date, // object
  typeof reg, // object
  typeof err, // object
  typeof fun, // function
  typeof und // undefined
);

2.使用instanceof检测

instanceof是通过__proto__来检测当前变量是否属于某个构造函数的

只有被new出来的才属于, null和undefined没有构造函数

console.log(
  num instanceof Number, // false
  str instanceof String, // false
  bool instanceof Boolean, // false
  obj instanceof Object, // true
  arr instanceof Array, // true
  nul instanceof Object, // false
  date instanceof Object, // true
  reg instanceof Object, // true
  err instanceof Object, // true
  fun instanceof Function, // true
  und instanceof Object // false
);

3.使用constructor检测

console.log(
  num.constructor, // Number
  str.constructor, // String
  bool.constructor, // Boolean
  obj.constructor, // Object
  arr.constructor, // Array
  date.constructor, // Date
  reg.constructor, // RegExp
  err.constructor, // Error
  fun.constructor // Function
  // nul.constructor, // null 没有constructor
  // und.constructor // undefined 没有constructor
);

4.Object.prototype.toString.call()

console.log(
  Object.prototype.toString.call(num), // [object Number]
  Object.prototype.toString.call(str), // [object String]
  Object.prototype.toString.call(bool), // [object Boolean]
  Object.prototype.toString.call(obj), // [object Object]
  Object.prototype.toString.call(arr), // [object Array]
  Object.prototype.toString.call(date), // [object Date]
  Object.prototype.toString.call(reg), // [object RegExp]
  Object.prototype.toString.call(err), // [object Error]
  Object.prototype.toString.call(fun), // [object Function]
  Object.prototype.toString.call(nul), // [object Null]
  Object.prototype.toString.call(und) // [object Undefined]
);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

宋哈哈

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

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

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

打赏作者

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

抵扣说明:

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

余额充值