The way of checking the type of an object
程序员文章站
2022-03-14 09:05:18
...
Apress ProJavaScriptTechniques
The first way of checking the type of an object is by using the obvious-sounding typeof operator.
js 代码
- // Check to see if our number is actually a string
- if ( typeof num == "string" )
- // If it is, then parse a number out of it
- num = parseInt( num );
- // Check to see if our array is actually a string
- if ( typeof arr == "string" )
- // If that's the case, make an array, splitting on commas
- arr = arr.split(",");
js 代码
- // Check to see if our number is actually a string
- if ( num.constructor == String )
- // If it is, then parse a number out of it
- num = parseInt( num );
- // Check to see if our string is actually an array
- if ( str.constructor == Array )
- // If that's the case, make a string by joining the array using commas
- str = str.join(',');
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 代码
- // Strictly check a list of variable types against a list of arguments
- function strict( types, args ) {
- // Make sure that the number of types and args matches
- if ( types.length != args.length ) {// If they do not, throw a useful exception
- throw "Invalid number of arguments. Expected " + types.length +
- ", received " + args.length + " instead.";
- }
- // Go through each of the arguments and check their types
- for ( var i = 0; i < args.length; i++ ) {
- //
- if ( args[i].constructor != types[i] ) {
- throw "Invalid argument type. Expected " + types[i].name +
- ", received " + args[i].constructor.name + " instead.";
- }
- }
- }
- // A simple function for printing out a list of users
- function userList( prefix, num, users ) {
- // Make sure that the prefix is a string, num is a number,
- // and users is an array
- strict( [ String, Number, Array ], arguments );
- // Iterate up to 'num' users
- for ( var i = 0; i < num; i++ ) {
- // Displaying a message about each user
- print( prefix + ": " + users[i] );
- }
- }
推荐阅读
-
PHP错误Cannot use object of type stdClass as array in错误的解决办法
-
PHP JSON出错:Cannot use object of type stdClass as array解决方法
-
详解Python中的type和object
-
TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type s
-
TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type s
-
SLF4J: Failed toString() invocation on an object of type [com.zhao.guang.xiao.top.po.BlogBean$Hibern
-
软件构造 3-1 Data Type and Type Checking
-
[VUE ERROR] Invalid default value for prop "slides": Props with type Object/Array must use a factory function to return the default value
-
Python 对象(type/object/class) 作用域 一等函数 (慕课--Python高级,IO并发 第二章)
-
MyBatis Generator报错:Cannot instantiate object of type(问题解决)