类型判断&toString

typeof

操作符返回一个字符串,表示未经计算的操作数的类型

typeof undefined //unedfined
typeof null //object
typeof true //boolean
typeof 'a' //string
typeof Symbol(1) //symbol
typeof fn //function
typeof {} //object
typeof [] //object

typeof String('a') //string
typeof new String('a') //object

instanceof

instanceof运算符用于测试构造函数的prototype属性是否出现在对象的原型链中的任何位置

function A(){}
var a = new A()
a instanceof A //true
a instanceof Object //true

'a' instanceof String //false
String('a') instanceof String //false
new String('a') instanceof String //true

1 instanceof Number //false
new Number(1) instanceof Number //false

true instanceof Boolean //false

null instanceof Object //false

var fn = function(){}
fn instanceof Function //true
fn instanceof Object //true

[] instanceof Array //true
[] instanceof Object //true

Object.prototype.toString.call

方法返回一个表示该对象的字符串

Object.prototype.toString.call(1) //"[object Number]"
Object.prototype.toString.call('a') //"[object String]"
Object.prototype.toString.call(undefined) //"[object Undefined]"
Object.prototype.toString.call(null) //"[object Null]"
Object.prototype.toString.call([]) //"[object Array]"
Object.prototype.toString.call(String) //"[object Function]"
Object.prototype.toString.call(new String) //"[object String]"
Object.prototype.toString.call(new Date) //"[object Date]"
Object.prototype.toString.call(Math) //"[object Math]"

Array.isArray

用于确定传递的值是否是一个 Array

Array.isArray([1,2,3]) //true
Array.isArray({a:1}) //false

Array.isArray(new Array())//true
// 鲜为人知的事实:其实 Array.prototype 也是一个数组。
Array.isArray(Array.prototype) //true

Array.