typeof和instanceof 运算符
生活随笔
收集整理的这篇文章主要介绍了
typeof和instanceof 运算符
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
instanceof运算符与typeof运算符相似,都适用于检测数据类型,但是在具体细节方面还是不同的
使用typeof测试数据类型
typeof 12;//"number" typeof "12";//"string" typeof false;//"boolean" typeof null;//"object" typeof undefined;//"undefined"function f() {} typeof f"function"typeof {};//"object" typeof [];//"object" typeof null;//"object" typeof window;"object"从上面的地结果可以看出来,无论引用的是什么类型的对象,它都返回 "object"。 如果想要知道对象是什么具体类型该怎么办呢?
运算符 instanceof 可以解决这个问题,与 typeof 方法不同的是,instanceof 方法要求开发者明确地确认对象为某特定类型。
实例:
var oStringObject = new String("hello world"); alert(typeof(oStringObject)); //输出 "object" alert(oStringObject instanceof String); //输出 "true"结论:如果变量是基本数据类型,使用typeof非常方便,但是要知道object对象的具体引用类型就需要使用instanceof了
转载于:https://www.cnblogs.com/YeChing/p/6243744.html
总结
以上是生活随笔为你收集整理的typeof和instanceof 运算符的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: codevs 1082 线段树区间求和
- 下一篇: js typeof