The class property of the object (class attribute) is a string that represents the type information of the object. Both ECMASCRIPT3 and ECMAScript5 do not provide a way to set this property, and there is only one indirect way to query it. The default ToString () Method (inherited from Object.prototype) returns a string in the following format:
[Object class]
Such as:
"[Object Null]", "[Object Undefined]", "[Object String]", "[Object number]", "[Object Array]", "[Object RegExp]", "[Object Boolean] "
Therefore, to get the class of an object, you can call the object's ToString () method, and then extract the characters from the 8th to the penultimate position in the returned character. But the tricky thing is that many of the object-inheriting ToString () methods have rewritten that the Function.call () method must be called indirectly in order to invoke the correct ToString () version. The following classof () function can return the class of any object passed to it:
1 function classof (o) {2 if (o==nullreturn "null"; 3 if return "Undefined"; 4 return Object.prototype.toString.call (o). Slice (applicationcache,-1); 5 }
Such as:
Classof (NULL)//"NULL"
Classof (1)//"number"
Classof ("")//"String"
From the JavaScript authoritative guide 6.8.2
JavaScript class Properties