Example of JavaScript object reflection usage

  • 2020-05-30 19:24:13
  • OfStack

This article illustrates the use of JavaScript object reflection as an example. Share with you for your reference. The details are as follows:

This article describes the use of reflection of JavaScript objects, including reflection of DOM objects and custom objects


<html>
<head>
<title>JavaScript Reflection tool </title>
<style type="text/css">
#show{
width:400px;height:300px;
border:red solid 1px;
overflow:scroll;
}
#main{
width:500px;
text-align:left;
margin-left:auto;
margin-right:auto;
}
</style>
<script type='text/javascript'>
// Generate the selected reflection object and reflect 
function SwitchObj(){
 var obj;
 var switchobj=document.getElementById('selects');
 if(switchobj.value=="op_div"){
      obj=document.createElement("div");
  }
 if(switchobj.value=="op_select"){
      obj=document.createElement("select");
  }
 if(switchobj.value=="op_p"){
      obj=document.createElement("p");
  }
 if(switchobj.value=="op_span"){
      obj=document.createElement("span");
  }
 if(switchobj.value=="op_table"){
      obj=document.createElement("table");
  }
 if(switchobj.value=="op_tr"){
      obj=document.createElement("table");
  }
 if(switchobj.value=="op_window"){
      obj=document.createElement("window");
  }
 if(switchobj.value=="op_document"){
      obj=document.createElement("document");
  }
 Assembly(obj);
}
// Reflection object 
function Assembly(obj){
  var order=0;
  if(obj){
    var assstr=" Reflection object :"+obj.tagName+"<br/>"
    for(key in obj){
      order++;
      assstr+=order+"----"+key+"<br/>";
    }
    Show(assstr);
  }
}
// Output the reflected information 
function Show(msg){
  var showobj=document.getElementById('show');
  if(showobj){
    showobj.innerHTML="";
    showobj.innerHTML=msg;
   }
}
</script>
</head>
<body>
<div id="main">
<h1>JavaScript Reflection tool </h1>
<div id="show"></div>
<input type="button" id="btn_assembly" value=" reflection " 
onclick="SwitchObj('select');" />
<select id="selects">
<option value='op_div'>div</option>
<option value='op_p'>p</option>
<option value='op_span'>span</option>
<option value='op_table'>table</option>
<option value='op_select'>select</option>
<option value='op_document'>document</option>
<option value='op_window'>window</option>
</select>
</div>
</body>
</html>

I hope this article has been helpful to your javascript programming.


Related articles: