function Getdatatype (any)
{
/* (1) Object.prototype.toString.call method judgment type:
Pros: Generic, returns the type of "[Object String]" specific object
Disadvantage: Cannot return an inherited type
(2) typeof X
Disadvantage: The object type can not be subdivided;
Advantages: The application of the judgment of NULL null ' undefined ';
The return type has: ' Undefined ' "string" ' Number ' ' Boolean ' function ' ' object '
(3) instanceof can return the specific type, only for the object created with the New keyword to judge
*/
var basetype=["string", "Number", "Boolean"];//basic type
var reftype=["Object", "Function", "Array", "Date"];//reference type
var dtype=object.prototype.tostring.call (any);
if (dtype== "[Object Object]")//ie, a DOM element object
{
if (any.constructor)
{
var constructorstr=any.constructor.tostring (); Obj.constructor can return an inherited type
if (Constructorstr.indexof ("array")!=-1) {dtype= "[object array]";}
Else
if (Constructorstr.indexof ("Htmlcollection")!=-1) {/* IE */dtype= "[Object NodeList]";}
Else
if ((Constructorstr.indexof ("function")!=-1) && (Constructorstr.indexof ("Object ()")!=-1))
{
Dtype= "[Object Object]";
}
Else
DTYPE=CONSTRUCTORSTR;
}
}
Else
{
if (dtype== "[Object Htmlcollection]") {/* FF */dtype= "[Object NodeList]";}
}
return dtype;
}
Convert nodelist to arrays so that nodelist can use all the methods of the array
function Nodelisttoarray (nodes) {
var array = null;
try{
Array = Array.prototype.slice.call (nodes,0);
}catch (ex) {
Array = new Array ();
for (var i = 0,len = Nodes.length;i < len;i++) {
Array.push (Nodes[i]);
}
}
return array;
}
/*//Test Getdatatype method
var fn=function () {}
Alert (Getdatatype (FN)); [Object Function]
Alert (Getdatatype (New Object ())); [Object Object]
Alert (Getdatatype ("Hello"));//[object String]
Alert (Getdatatype (234));//[object number]
Alert (Getdatatype (true));//[object Boolean]
Alert (Getdatatype (New Date ())); [Object Date]
Alert (Getdatatype (New Date (). GetTime ())); [Object number]
Alert (Getdatatype (document.getElementById ("Demopic")); [Object Htmldivelement]
var Nodelist=nodelisttoarray (document.getElementsByTagName ("*"));
Alert (Getdatatype (nodelist)); [Object Array]
Alert (Getdatatype (document.getElementsByTagName ("*"))); [Object NodeList)]
alert (nodelist[10].tagname);
*/
JS Gets or judges the generic method of any data class type (Getdatatype) and converts the nodelist to an array (Nodelisttoarray)