javascript中的instance和typeof的异同点

typeof的用法

typeof用在判断数据类型上,数据类型分为值类型和引用类型。

值类型包括4种:undefined、string、number、boolean
引用类型包括2种:function、object (其中需要注意的是typeof null==object)

instanceof的用法

instanceof操作符用来判断引用类型。

语法:A instanceof B (一般用在判断原型链上的关系)

判断规则:沿着A的proto属性这条线来找,同时沿着B的prototype属性这条线,如果两条线能找到同一个引用,即 同一个对象,则返回true。

例如:

   alert( Object instanceof Function)    //true

   alert(Function  instanceof Object)    //true

   alert( Function instanceof Function)   //true

猜你喜欢

转载自blog.csdn.net/mm_hello11/article/details/80043580