js判断变量类型 有2种方法
1.使用typeof
2.使用Variables.Constructor
Example:
- <script type="text/javascript">
- function fun(msg)
- {
- //使用typeof判断
- if(typeof msg=="string")
- {
- alert("使用typeof判断:"+msg);
- }
- //使用constructor判断
- if(msg.constructor==String)
- {
- alert("使用constructor判断"+msg);
- }
- }
- fun("aa");
- </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 |
本文介绍JavaScript中两种常见的变量类型检查方法:使用typeof操作符和constructor属性。通过具体示例展示了如何利用这两种方式来判断变量的具体类型。
5779

被折叠的 条评论
为什么被折叠?



