The results of type checking for different types of objects, respectively, using the two methods I described. The first column of the table shows the objects that we are trying to find their type. Each two column is run typeof variable (variable is the value shown in the first column). All the results in this column are strings. Finally, the third column shows the results of running variable.constructor on the objects contained in the first column. All the results in some columns are objects.
Table 1. Variable type check
———————————————————————————————
Variable typeof variable Variable.constructor
———————————————————————————————
{an: "Object"} Object
["An", "array"] Object array
function () {} function function
"A string" string string
Number number
True Boolean Boolean
New user () object user
——————————————————————————————————
Using the constructor of a variable as a reference to an object type may be the simplest type of checking method
Check if our numbers are actually a string
if (Num.constructor = = string)
If it is, parse it into a number
num = parseint (num);
Check whether our string is actually an array
if (Str.constructor = = array)
If so, connect the array with a comma and get a string
str = Str.join (', ');
Gets the byte length of the string
*/
Java code
1. function strlen (str)
2. {
3. var i;
4. var Len;
5.
6. Len = 0;
7. for (i=0;i<str.length;i++)
8. {
9. If (Str.charcodeat (i) >255) len+=2; else len++;
10.}
One. return len;
12.}
13.
function strlen (str)
{
var i;
var Len;
len = 0;
for (i=0;i<str.length;i++)
{
if (Str.charcodeat (i) >255) len+=2; else len++;
}
return Len;
}