补充一下,通常来讲,使用 instanceof 就是判断一个实例是否属于某种类型,更重要的是 instanceof 可以在继承关系中用来判断一个实例是否属于它的父类型。
用 instanceof 来判断类型只能用于对象层面,不是一个对象则不行:
str ="asd";
if(str instanceofString)//返回false
document.write("YES");//不会打印YES
str =newString();
if(str instanceofString)//返回true
document.write("yes");//不会打印yes