共计 887 个字符,预计需要花费 3 分钟才能阅读完成。
js 的数据类型
根本数据类型:number , string , boolean , undefined , null , Symbol,
援用数据类型:object
NaN 属于 number;
Function, Array, Date 都属于 object;
根本数据类型除 null 都能够通过 typeof 判断,援用数据类型除 Function 外都返回 Ojbect
let a = 1,
b = '2',
c = true,
d = undefined,
e = null,
f = Symbol('f'),
g = function () {},
h = [],
i = new Date()
console.log(typeof a)
console.log(typeof b)
console.log(typeof c)
console.log(typeof d)
console.log(typeof e)
console.log(typeof f)
console.log(typeof g)
console.log(typeof h)
console.log(typeof i)
查看输入后果
能够看到 null 的 typeof 是 object , 这属于历史 bug,有趣味能够参考《The history of“typeof null”》
可通过以下办法判断 null
function checkNull(num) {return num === null}
object 的具体类型可通过 Object.prototype.toString.call() 判断
function checkObject(obj) {return Object.prototype.toString.call(obj)
}
console.log(checkObject(g))
console.log(checkObject(h))
console.log(checkObject(i))
可看到输入后果
也可通过构造函数 constructor() 判断
console.log(g.constructor === Function)
console.log(h.constructor === Array)
console.log(i.constructor === Date)
可看到输入后果
正文完