JS的类型判断

73 阅读1分钟

大家好,我是小逗号,今天给大家分享一下我们前端JS的类型判断

js类型分为两种

简单类型 : Number , String , Boolean , Undefined , Null ,symbol 。

复杂类型 : object(Array , Function ,Date)。

在我们的内存存储分为堆内存和栈内存,也就是简单类型存储栈,复杂存储堆里面。

呢么我们接下来说一下它们的类型判断。

1. typeof 不可以判断复杂数据类型 复杂数据类型返回Object

let typeof_ = {
  isArray:(context)=>typeof context , 
  isObject:(context)=>typeof context ,
  isString:(context)=>typeof context ,
  isNull:(context)=>typeof context ,
  isUndefined:(context)=>typeof context ,
  isNumber:(context)=>typeof context ,
  isBoolean:(context)=>typeof context ,
  isFunction:(context)=>typeof context ,
  isSymbol:(context)=>typeof context ,
  isDate:(context)=>typeof context ,
}

console.log(typeof_.isArray([])); // object
console.log(typeof_.isObject({})); // object
console.log(typeof_.isString("1111")); // string
console.log(typeof_.isNull(null)); // object
console.log(typeof_.isUndefined(undefined)); // undefined
console.log(typeof_.isNumber(111)); // number
console.log(typeof_.isBoolean(true)); // boolean
console.log(typeof_.isFunction(new Function())); // function
console.log(typeof_.isSymbol(Symbol())); // symbol
console.log(typeof_.isDate(new Date())); // object

2. instanceof 不可以检测简单数据类型 null undefined 也不可以

let instanceof_ = {
  isArray:(context)=>  context instanceof Array , 
  isObject:(context)=> context instanceof Object,
  isString:(context)=> context instanceof String ,
  isNumber:(context)=> context instanceof Number,
  isBoolean:(context)=> context instanceof Boolean,
  isFunction:(context)=> context instanceof Function,
  isSymbol:(context)=> context instanceof Symbol,
  isDate:(context)=> context instanceof Date, 
}

console.log(instanceof_.isArray([])); // true
console.log(instanceof_.isObject({})); // true
console.log(instanceof_.isString("1111")); //  false
console.log(instanceof_.isNumber(111)); // false
console.log(instanceof_.isBoolean(true)); // false
console.log(instanceof_.isFunction(new Function())); // true
console.log(instanceof_.isSymbol(Symbol())); // false
console.log(instanceof_.isDate(new Date())); //  true

3. constructor null 与 undefined 没有**

let constructor_ = {
  isArray:(context)=>  context.constructor === Array , 
  isObject:(context)=> context.constructor === Object,
  isString:(context)=> context.constructor === String ,
  isNumber:(context)=> context.constructor === Number,
  isBoolean:(context)=> context.constructor === Boolean,
  isFunction:(context)=> context.constructor === Function,
  isSymbol:(context)=> context.constructor === Symbol,
  isDate:(context)=> context.constructor === Date, 
}

console.log(constructor_.isArray([])); // true
console.log(constructor_.isObject({})); // true
console.log(constructor_.isString("1111")); // true
console.log(constructor_.isNumber(111)); // true 
console.log(constructor_.isBoolean(true)); // true
console.log(constructor_.isFunction(new Function())); // true
console.log(constructor_.isSymbol(Symbol())); // true
console.log(constructor_.isDate(new Date())); // true

4. toString 对象判断类型 toString Object 类型可以直接调用 toString() 其它需要用 call 或者 apply 来调用

let prototypeToString_ = {
  isArray:(context)=>Object.prototype.toString.call(context)==="[object Array]", 
  isObject:(context)=>Object.prototype.toString.call(context)==="[object Object]",
  isString:(context)=>Object.prototype.toString.call(context)==="[object String]",
  isNull:(context)=>Object.prototype.toString.call(context)==="[object Null]",
  isUndefined:(context)=>Object.prototype.toString.call(context)==="[object Undefined]",
  isNumber:(context)=>Object.prototype.toString.call(context)==="[object Number]",
  isBoolean:(context)=>Object.prototype.toString.call(context)==="[object Boolean]",
  isFunction:(context)=>Object.prototype.toString.call(context)==="[object Function]",
  isSymbol:(context)=>Object.prototype.toString.call(context)==="[object Symbol]",
  isDate:(context)=>Object.prototype.toString.call(context)==="[object Date]",
}

console.log(prototypeToString_.isArray([])); // true
console.log(prototypeToString_.isObject({})); // true
console.log(prototypeToString_.isString("111")); // true
console.log(prototypeToString_.isNull(null)); // true
console.log(prototypeToString_.isUndefined(undefined)); // true
console.log(prototypeToString_.isNumber(11)); // true
console.log(prototypeToString_.isBoolean(true)); // true
console.log(prototypeToString_.isFunction(new Function())); // true
console.log(prototypeToString_.isSymbol(Symbol())); // true
console.log(prototypeToString_.isDate(new Date())); // true

喜欢的话给个赞哦,还会有更多前端小知识