美文网首页js
类型判断

类型判断

作者: 马甲要掉了 | 来源:发表于2020-05-19 23:05 被阅读0次

js中的类型:

  • 基本数据类型:Undefined、Null、Boolean、Number、String,Symbol
  • 引用数据类型 :Object
let bool = true;
let num = 1;
let str = 'abc';
let und= undefined;
let nul = null;
let s1 = Symbol();
let arr = [1,2,3,4];
let obj = {name:'xiaoming',age:22};
let fun = function(){console.log('hello')};
  1. typeof
console.log(typeof bool); //boolean
console.log(typeof num);//number
console.log(typeof str);//string
console.log(typeof und);//undefined
console.log(typeof s1); //symbol
console.log(typeof nul);//object
console.log(typeof arr);//object
console.log(typeof obj);//object
console.log(typeof fun);//function

typeof可以识别出除null以外的基本数据类型,只能识别出为function的引用类型。
(typeof可以识别出基本类型boolean,number,undefined,string,symbol,但是不能识别null。不能识别引用数据类型,会把null、array、object统一归为object类型,但是可以识别出function)
typeof 返回的是string typeof (typeof null) === 'string'

  1. instanceof
console.log(bool instanceof Boolean);// false
console.log(num instanceof Number);// false
console.log(str instanceof String);// false
console.log(und instanceof Object);// false
console.log(nul instanceof Object);// false
console.log(s1 instanceof Symbol);// false
console.log(arr instanceof Array);// true
console.log(obj instanceof Object);// true
console.log(fun instanceof Function);// true

instanceof不能识别出所有基本数据类型,但是可以检测出引用类型,并且可以检测出多层继承关系。(js的继承都是采用原型链来继承的。比如objA instanceof A ,其实就是看objA的原型链上是否有A,而A的原型上保留A的constructor属性。)

如: image.png
  1. constructor
console.log(bool.constructor === Boolean);// true
console.log(num.constructor === Number);// true
console.log(str.constructor === String);// true
console.log(s1.constructor === Symbol);//true
console.log(arr.constructor === Array);// true
console.log(obj.constructor === Object);// true
console.log(fun.constructor === Function);// true
基本类型的constructor

null、undefined没有construstor方法,因此constructor不能判断undefined和null。
但这是不安全的,因为contructor的指向是可以被改变。

  1. Object.prototype.toString().call()
console.log(Object.prototype.toString.call(bool));//[object Boolean]
console.log(Object.prototype.toString.call(num));//[object Number]
console.log(Object.prototype.toString.call(str));//[object String]
console.log(Object.prototype.toString.call(und));//[object Undefined]
console.log(Object.prototype.toString.call(nul));//[object Null]
console.log(Object.prototype.toString.call(arr));//[object Array]
console.log(Object.prototype.toString.call(obj));//[object Object]
console.log(Object.prototype.toString.call(fun));//[object Function]
console.log(Object.prototype.toString.call(s1)); //[object Symbol]
console.log(Object.prototype.toString.call(/a/g)); //[object RegExp]
....//能判断的类型非常多
  • 总结:
    至于在项目中使用哪个判断,还是要看使用场景,具体的选择,一般基本的类型可以选择typeof,引用类型可以使用instanceof。

相关文章

  • NodeJs实用技巧

    判断数组类型为 Array 判断对象类型为 Object 判断对象类型为 Number

  • 饿了吗大前端阅读(一)

    类型判断 我记得的类型判断函数就是 typeOf:判断基本类型和Object instanceof:判断是否是指定...

  • JavaScript - 4.数据类型判断

    数据类型判断 节点类型 nodeType 数据类型 typeof 方法 数组 Array 的判断 非数字的判断

  • 类型判断和JSON判断

    类型判断 JSON判断

  • 类型判断

    is as? as! Any 用法: AnyObject

  • 判断类型

    类型 JavaScript 中有七种内置类型: 空值 (null)未定义 (undefined)布尔值 (bool...

  • 类型判断

    typeof操作符 typeof返回一个表示数据类型的字符串,返回结果包括:number、string、boole...

  • 类型判断

  • 类型判断

    js中的类型: 基本数据类型:Undefined、Null、Boolean、Number、String,Symbo...

  • 类型判断

    检查当前元素的类型 (传入的type首字母可以小写)

网友评论

    本文标题:类型判断

    本文链接:https://www.haomeiwen.com/subject/jxfwohtx.html