One, JavaScript to determine the object type 1, you can use the TypeOf function to determine the object type
1 function CheckObject1 () {2 var str= "str"; 3 Console.log (typeof(str))// Output "string"; 4 Console.log (typeof(str) = = "string")// output true; 5 }?
2. Use the object's constructor property (constructor) to determine the object's type:
1 function CheckObject2 () {2 var str = "str"; 3 Console.log (str.construtor== "string")// output 4 }
3, the difference between the two
The TypeOf function can only be judged by the type of object:
(1) Boolean
(2) function
(3) Number
(4) Object
(5) string
(6) undefined
The use of the Construtor property can be used to determine a complex type. Like what
1 //a complex type of object that determines its type2 functionUser (name, age) {3 This. name=name;4 This. age=Age ;5 }6 functioncheckcomplexobj () {7 varS= ' A string ';8 varArr=[];9 varobj=NewObject ();Ten varUsr=NewUser ();//Custom Objects OneConsole.log (' s.constructor==string: ' + (s.constructor==string));//Output True AConsole.log (' Arr.constructor==array: ' + (Arr.constructor==array));//Output True -Console.log (' Obj.constructor==object: ' + (obj.constructor==object));//Output True -Console.log (' Usr.constructor.name==user: ' + (Usr.constructor.name==user));//Output True the}
Second, use JavaScript to determine the node type, node name and node value 1, node type classification
node Type |
Description |
value |
ELEMENT node |
Each HTML tag is an element node, such as <div>, <p>, <ul>, and so on |
1 |
Attribute node |
Attributes of ELEMENT nodes (HTML tags), such as ID, class, name, and so on. |
2 |
Text node |
The text content in the element node or attribute node. |
3 |
Comment Node |
Represents a document comment in the form <!--comment text---. |
8 |
Document node |
Represents the entire document (the root node of the DOM tree, which is document) |
9 |
2. Use JavaScript to determine node type, node name and node value
1<!--use JavaScript to determine the node type--2<div id= "Onediv" > a text </div><!--comment text--3<script type= "Text/javascript" >4 vardiv = document.getElementById ("Onediv");5Console.log (Div.nodetype);//output 1, element node6 varDivtext =Div.firstchild;7Console.log (Divtext.nodetype)//output 3, text node8 varDivattr = Div.getattributenode ("id");9Console.log (Divattr.nodetype)//output 2, attribute nodeTen varComment =div.nextsibling; OneConsole.log (Comment.nodetype)//output 8, note node A</script>
1<!--using JavaScript to determine node names--2<div id= "Onediv" > a text </div><!--comment text--3<script type= "Text/javascript" >4 vardiv = document.getElementById ("Onediv");5Console.log (Div.nodename);//output div, element node is tag uppercase6 varDivtext =Div.firstchild;7Console.log (Divtext.nodename)//output #text, text nodes are always #text when using nodename8 varDivattr = Div.getattributenode ("id");9Console.log (Divattr.nodename)//output ID, attribute node is property nameTen varComment =div.nextsibling; OneConsole.log (Comment.nodename)//output #comment, note nodes are always #comment when using nodename A</script>
1<!--use JavaScript to determine node values--2<div id= "Onediv" > a text </div><!--comment text--3<script type= "Text/javascript" >4 vardiv = document.getElementById ("Onediv");5Console.log (Div.nodevalue);//output NULL, element node not supported for NodeValue6 varDivtext =Div.firstchild;7Console.log (Divtext.nodevalue)//output a text, text node output text value8 varDivattr = Div.getattributenode ("id");9Console.log (Divattr.nodevalue)//output Onediv, attribute node output property valueTen varComment =div.nextsibling; OneConsole.log (Comment.nodevalue)//output comment text, comment node output comment content A</script>
JavaScript determines object type and node type, node name, and node value