首页 > 其他分享 >js变量类型判断

js变量类型判断

时间:2023-03-08 10:15:00浏览次数:42  
标签:instanceof 判断 变量 Object object js typeof constructor prototype

js变量类型判断方式

  • 首先我们知道js中若想验证某个值是否为null,应该使用操作符=====无法区分nullundefined;
  • 定义一组变量,适用于全文;
let num  = 123;
let num1 = 1 / 0  //Infinity
let num2 = null / 0  //NaN
let str  = 'hello';
let bool = true;
let arr  = [1, 2, 3];
let obj = {name:'zhangsan', age:22};
let func = function(){ console.log('normal function'); }
let func1 = () => { console.log('arrow function'); }
let und  = undefined;
let nul  = null;
let date = new Date();
let reg  = /-?\d+/;
let error= new Error();

typeof

typeof运算符用于判断对象的类型,但是对于一些创建的对象 和 引用类型,他们都会返回object;

console.log(
  typeof num, //number
  typeof num1, //number
  typeof num2,  //number
  typeof str,  //string
  typeof bool,  //boolean
  typeof arr,  // object
  typeof obj,  // object
  typeof func,  //function
  typeof func1,  //function
  typeof und,  //undefined
  typeof nul,  //object
  typeof date,  //object
  typeof reg,  //object
  typeof error  //object
);
  • infinity,NaN都被判断成了number类型;
  • 引用类型和js自有的类arr,reg,date,obj,nul,error都被判断成了object

instanceof

instance 运算符与typeof运算符类似,用于识别正在处理的对象的类型。instanceof运算符用来判断一个构造函数的prototype属性所指向的对象是否存在另一个要检测对象的原型链上,使用方式为A instanceof B,返回一个布尔值;

console.log(
  num instanceof Number,  //false
  num1 instanceof Number,  //false
  num2 instanceof Number,  //false
  str instanceof String,  //false
  bool instanceof Boolean,  //false
  arr instanceof Array,  //true
  obj instanceof Object, //true
  func instanceof Function,  //true
  func1 instanceof Function, //true
  und instanceof Object,  //false
  nul instanceof Object,  //false
  date instanceof Date,  //true
  reg instanceof RegExp,  //true
  error instanceof Error, //true
  date instanceof Date,  //true
);
  • num, strbool没有检测出类型,但是如果使用 new 的方式创建num, strbool,是可以检测出类型的,因为这是构造函数创建;
  • instanceof表示的是一种继承关系,或者原型链的结构;

constructor

constructor本来是原型对象上的属性,指向构造函数,根据实例对象寻找属性的顺序,若实例对象上没有实例属性或方法,就去原型链上寻找,所以实例对象也可以使用constructor属性,若输出一下num.constructor的内容,得到数字类型的变量的构造函数指向了Number的构造函数,因此,我们可以使用num.constructor==Number来判断num是不是Number类型的;

console.log(
  num.constructor== Number,
  str.constructor== String,
  bool.consructor== Boolean,

  arr.constructor== Array,
  json.constructor== Object,
  func.constructor== Function,
  date.constructor== Date,
  reg.constructor== RegExp,
  error.constructor== Error,
  // und.constructor ==  Undefined,  //Cannot read properties of undefined (reading 'constructor')
  // nul.constructor == Null,  //Cannot read properties of null (reading 'constructor')

);
  • 除了undefinednull,其他类型的变量均能使用constructor判断出类型,因为undefinednull的原型对象上没有定义constructor属性;
  • 但是由于原型对象的constructor属性是可以被修改的,并且对于引用类型的数据不同地方定义的原生对象所引用的地址是不一样的,所对应的构造函数也不一样,所以用它不保险;

Object.prototype.toString.call

原生js中的Object.prototype.toString.call()输出的是一个字符串,字符串里有一个数组,第一个参数是object,第二个参数就是这个变量的类型;

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

标签:instanceof,判断,变量,Object,object,js,typeof,constructor,prototype
From: https://www.cnblogs.com/rain111/p/17190990.html

相关文章