js的typeof

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38594777/article/details/79099710

js的typeof

typeof可以查看变量的类型

typeof( 123 );
typeof 123;
//typeof的形式这两种都可以,但一般用第一种
在控制台使用将打印出"number",可知123为number类型
typeof("ad")//控制台将打印string类型
typeof( ad );//控制台将打印undefined,可知ad为undefined类型
typeof(true)//控制台将打印boolean

typeof( function test(){ } )
//控制台将打印function
typeof( new  String() )
//控制台将打印出object类型
typeof(undefined)//控制台将打印undefined
typeof( null )
//但是控制台无法打印出null类型,控制台打印的是object类型
typeof的不足

typeof无法打印出null类型

number和string类型有其实两种类型,一是基础类型,另一是引用类型。typeof只能打印number和string的基础类型,无法打印引用的Number类型,他把引用类型都归于object,后期我们可以封装自己的typeof方法完善








猜你喜欢

转载自blog.csdn.net/qq_38594777/article/details/79099710
今日推荐