js判断变量类型(2种)

来源:互联网 发布:手机淘宝5.7.2版本安卓 编辑:程序博客网 时间:2024/05/17 08:18

js判断变量类型 有2种方法

1.使用typeof

2.使用Variables.Constructor

 

Example:

  1. <script type="text/javascript">
  2.     function fun(msg)
  3.     {
  4.         
  5.         //使用typeof判断
  6.         if(typeof msg=="string")
  7.         {
  8.             alert("使用typeof判断:"+msg);
  9.         }
  10.         //使用constructor判断
  11.         if(msg.constructor==String)
  12.         {
  13.             alert("使用constructor判断"+msg);
  14.         }
  15.     }
  16.     fun("aa");
  17. </script>

备注:

Type-Checking Variables

Variable

 typeof Variable

Variable.constructor

{ an:“object” }

object

Object

[ “an”,“array” ]

object    

Array

function() {}

function  

Function

“a string”

string

String

55

number

Number

True

boolean

Boolean

new User()

object        

User

      

                   

        

                  

                                                   

                                                                

                                                             

 

原创粉丝点击