一,js代码:
<html> <head> <meta charset="utf-8"/> <title>测试</title> </head> <body> <script> //一,用typeof 得到变量的类型 let one = 'a'; console.log(typeof one); one = 1; console.log(typeof one); one = true; console.log(typeof one); one = Symbol(); console.log(typeof one); one = undefined; console.log(typeof one); one = new Function(); console.log(typeof one); //二,用toString.call得到变量的类型 one = 'a'; console.log(typeof one); console.log(toString.call(one)); one = 1; console.log(typeof one); console.log(toString.call(one)); one = true; console.log(typeof one); console.log(toString.call(one)); one = Symbol(); console.log(typeof one); console.log(toString.call(one)); one = undefined; console.log(typeof one); console.log(toString.call(one)); one = new Function(); console.log(typeof one); console.log(toString.call(one)); //说明:可以看到 对象,null,数组,Date,Error等被typeof统一识别成 object // 这种情况下使用toString.call更准确 one = null; console.log(typeof one); console.log(toString.call(one)); one = {}; console.log(typeof one); console.log(toString.call(one)); one = [1,2,3]; console.log(typeof one); console.log(toString.call(one)); one = new RegExp(); console.log(typeof one); console.log(toString.call(one)); one = new Date(); console.log(typeof one); console.log(toString.call(one)); one = new Error(); console.log(typeof one); console.log(toString.call(one)); one = window; console.log(typeof one); console.log(toString.call(one)); one = document; console.log(typeof one); console.log(toString.call(one)); //三,用instanceof判断是否某个类的实例: function A(name,age){ this.name = name; this.age = age; } let a = new A('张三',18); console.log(a instanceof A); class myClass{ constructor(name,age){ this.name=name this.age=age } } let i=new myClass('小明',18); console.log(i instanceof myClass); console.log(i instanceof A); console.log(typeof i); console.log(toString.call(i)); //四,constructor方法,因为不支持undefined和null,不再举例 </script> </body> </html>
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
或: https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: [email protected]
二,测试效果
三,查看chrome的版本:
标签:5195.125,console,log,chrome,javascript,toString,typeof,new,call From: https://www.cnblogs.com/architectforest/p/16758567.html