The way of checking the type of an object

本文介绍了JavaScript中两种类型检查的方法:使用typeof操作符和constructor属性。通过示例展示了如何确保变量类型正确,以及如何严格检查函数参数类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Apress ProJavaScriptTechniques

The first way of checking the type of an object is by using the obvious-sounding typeof operator.

js 代码
 
  1. // Check to see if our number is actually a string  
  2. if ( typeof num == "string" )  
  3. // If it is, then parse a number out of it  
  4. num = parseInt( num );  
  5. // Check to see if our array is actually a string  
  6. if ( typeof arr == "string" )  
  7. // If that's the case, make an array, splitting on commas  
  8. arr = arr.split(",");  
The second way of checking the type of an object is by referencing a property of all JavaScript objects called constructor.
js 代码
 
  1. // Check to see if our number is actually a string  
  2. if ( num.constructor == String )  
  3. // If it is, then parse a number out of it  
  4. num = parseInt( num );  
  5. // Check to see if our string is actually an array  
  6. if ( str.constructor == Array )  
  7. // If that's the case, make a string by joining the array using commas  
  8. str = str.join(',');  
Table 2-1 shows the results of type-checking different object types using the two different methods that I’ve described.

Variable<o:p></o:p>

typeof Variable<o:p></o:p>

Variable.constructor<o:p></o:p>

{ an: “object” }<o:p></o:p>

object<o:p></o:p>

 Object<o:p></o:p>

[ “an”, “array” ]<o:p></o:p>

array<o:p></o:p>

Array<o:p></o:p>

function(){}<o:p></o:p>

function<o:p></o:p>

Function<o:p></o:p>

“a string”<o:p></o:p>

string<o:p></o:p>

String<o:p></o:p>

55<o:p></o:p>

number<o:p></o:p>

Number<o:p></o:p>

true<o:p></o:p>

boolean<o:p></o:p>

Boolean<o:p></o:p>

new User()<o:p></o:p>

object<o:p></o:p>

User<o:p></o:p>


Strict typechecking can help in instances where you want to make sure that exactly the right number of arguments of exactly the right type are being passed into your functions.
js 代码
 
  1. // Strictly check a list of variable types against a list of arguments  
  2. function strict( types, args ) {  
  3. // Make sure that the number of types and args matches  
  4. if ( types.length != args.length ) {// If they do not, throw a useful exception  
  5. throw "Invalid number of arguments. Expected " + types.length +  
  6. ", received " + args.length + " instead.";  
  7. }  
  8. // Go through each of the arguments and check their types  
  9. for ( var i = 0; i < args.length; i++ ) {  
  10. //  
  11. if ( args[i].constructor != types[i] ) {  
  12. throw "Invalid argument type. Expected " + types[i].name +  
  13. ", received " + args[i].constructor.name + " instead.";  
  14. }  
  15. }  
  16. }  
  17. // A simple function for printing out a list of users  
  18. function userList( prefix, num, users ) {  
  19. // Make sure that the prefix is a string, num is a number,  
  20. // and users is an array  
  21. strict( [ String, Number, Array ], arguments );  
  22. // Iterate up to 'num' users  
  23. for ( var i = 0; i < num; i++ ) {  
  24. // Displaying a message about each user  
  25. print( prefix + ": " + users[i] );  
  26. }  
  27. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值