0
点赞
收藏
分享

微信扫一扫

关闭ubuntu2204桌面

SDKB英文 2024-06-29 阅读 48

一、数据类型(8种)

  • 基本数据类型:number string Boolean undefined null symbol bigint
  • 引用数据类型:object (array function )

二、区分数据类型

1、typeof

  • 通常用来判断基本数据类型,它返回表示数据类型的字符串
  • 可以判断: undefined/ 数值 / 字符串 / 布尔值 / function
  • 不能判断: null object array

  typeof 1           //number
typeof 'a' //string
typeof true //boolean
typeof undefined //undefined

typeof null //object
typeof {} //object
typeof [1,2,3] //object
function Fn(){}
typeof new Fn() //function
typeof new Array() //object

2、instanceof

  • A instanceof B 可以判断A是不是B的实例
  • 返回一个布尔值
  • 由构造类型判断出数据类型
  • instanceof 后面一定要是对象类型,大小写不能写错,该方法试用一些条件选择或分支
  console.log(arr instanceof Array ); // true
console.log(date instanceof Date ); // true
console.log(fn instanceof Function ); // true

3、根据对象的contructor判断

  • 判断 数据类型 的 contructor 是不是 相应的 对象类型
  • 返回 布尔值 大小写不能写错
console.log('数据类型判断' -  constructor);
console.log(arr.constructor === Array); //true
console.log(date.constructor === Date); //true
console.log(fn.constructor === Function); //true

4、Object.prototype.toString.call()

  • 万能方法,判断原型
Object.prototype.toString.call();
console.log(toString.call(123)); //[object Number]
console.log(toString.call('123')); //[object String]
console.log(toString.call(undefined)); //[object Undefined]
console.log(toString.call(true)); //[object Boolean]
console.log(toString.call({})); //[object Object]
console.log(toString.call([])); //[object Array]
console.log(toString.call(function(){})); //[object Function]
举报

相关推荐

0 条评论