-
typeof
用在基础数据范例和函数时,返回其对应范例的形貌,关于援用范例都返回为object. -
instanceof
没法推断基础数据范例,关于援用范例数据,返回其其对应范例。 -
Object.prototype.toString
不管基础数据范例照样援用范例返回其对应范例。
对应测试效果以下:
typeof test | instanceof | Object.prototype.toString.call(test) | |
var test = ‘xuriliang’; | string | test instanceof String //false | [object String] |
var test = 27; | number | test instanceof Number //false | [object Number] |
var test = true; | boolean | test instanceof Boolean //false | [object Boolean] |
var test = [1,2,3]; | object | test instanceof Array //true | [object Array] |
test instanceof Object //true | |||
var test = null; | object | test instanceof Object //false | [object Null] |
var test = undefined; | undefined | test instanceof Object //false | [object Undefined] |
var test = new String(‘xuriliang’) | object | test instanceof String //true | [object String] |
test instanceof Object //true | |||
var test = new Number(27) | object | test instanceof Number //true | [object Number] |
test instanceof Object //true | |||
var test = new Boolean(true) | object | test instanceof Boolean //true | [object Boolean] |
test instanceof Object //true | |||
var test = new Array(1,2,3) | object | test instanceof Array //true | [object Array] |
test instanceof Object //true | |||
var test = function(){} | function | test instanceof Function //true | [object Function] |
test instanceof Object //true | |||
var test = /d/ | object | test instanceof RegExp //true | [object RegExp] |
test instanceof Object //true |