typeof instanceof constructor.name Object .prototype .toString .call ('str' )
typeof
用于查看基本数据的数据类型, number string boolean undefined
null 比较特殊,结果是 object
如果查看复杂数据类型,返回的都是 object 类型
函数的结果是 function
typeof 12 typeof 'abc' typeof true typeof undefined typeof null typeof function ( ) {} typeof [] typeof {}
instanceof 判断
object instanceof constructor
用来检测 constructor.prototype
是否存在于参数 object
的原型链中
不能用于类型识别
var simpleStr = 'This is a simple string' var myString = new String ()var newStr = new String ('String created with constructor' )var myObj = {}var myNonObj = Object .create (null )var myArr = []var myFn = function ( ) {}simpleStr instanceof String myString instanceof String newStr instanceof String myString instanceof Object myObj instanceof Object ;({} instanceof Object ) myNonObj instanceof Object myArr instanceof Array myArr instanceof Object myFn instanceof Object myFn instanceof Function
constructor.name
Undefined/Null 没有 constructor 属性
var myArr = []var myFn = function ( ) {}var myObj = {}let myDate = new Date ()myArr.constructor .name myFn.constructor .name myObj.constructor .name myDate.constructor .name function Teacher (name, age ) { this .name = name this .age = age } var tea = new Teacher ('zs' , 18 )tea.constructor .name
Object.prototype.toString
适用于任何类型的检测,不能识别自定义对象类型
Object .prototype .toString .call ('str' ).slice (8 , -1 ) function Teacher (name, age ) { this .name = name this .age = age } var tea = new Teacher ('zs' , 18 )Object .prototype .toString .call (tea)