JS Gets or judges the generic method of any data class type (Getdatatype) and converts the nodelist to an array (Nodelisttoarray)

Source: Internet
Author: User
Tags null null object object

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.