ITPub博客

首页 > IT职业 > IT图书 > js如何对类型进行判断

js如何对类型进行判断

IT图书 作者:wanxud 时间:2021-09-11 09:53:16 0 删除 编辑

1、判断引用类型和基本类型不同,判断基本类型可以使用typeof。

typeof对于引用类型,除了函数返回function外,还返回object。但是,我们开发的数组必须返回array类型,typeof对引用类型不太适用。

typeof 1 // 'number'
typeof '1' // 'string'
typeof undefined // 'undefined'
typeof true // 'boolean'
typeof Symbol() // 'symbol'
typeof null // 'object'

2、判断引用类型通常使用instanceof。

var obj = {}
var arr = []
var fun = () => {}
typeof obj // 'object'
typeof arr // 'object'
typeof fun // 'function'
obj instanceof Object // true
arr instanceof Array // true
fun instanceof Function // true

以上就是js对类型进行判断的方法,希望对大家有所帮助。更多js学习指路:js教程

推荐操作环境:windows7系统、jquery3.2.1版本,DELL G3电脑。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3451/viewspace-2829611/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-30

  • 博文量
    100
  • 访问量
    1144