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
本文详细介绍了JavaScript中typeof和instanceof操作符的使用方法。typeof用于判断数据类型,包括值类型(如undefined, string, number, boolean)和引用类型(如function, object)。instanceof则用于检查对象是否为某个构造函数的实例,通过比较原型链实现。
2407

被折叠的 条评论
为什么被折叠?



