检测JavaScript 中各种类型

2017年12月30日

 1Object.prototype.toString.call({})
 2// => "[object Object]"
 3
 4Object.prototype.toString.call('')
 5// => "[object String]"
 6
 7Object.prototype.toString.call([])
 8// => "[object Array]"
 9
10Object.prototype.toString.call(1)
11// => "[object Number]"
12
13Object.prototype.toString.call(/a/)
14// => "[object RegExp]"
15
16Object.prototype.toString.call(new Date())
17// => "[object Date]"
18
19Object.prototype.toString.call(new Error())
20// => "[object Error]"