Last active
January 13, 2016 22:41
-
-
Save bzdgn/1ed7f4780c000e802f43 to your computer and use it in GitHub Desktop.
JavaScript Demo For Displaying Object Properties and Attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<h1>Object Properties Example #3</h1> | |
<br> | |
<h3>Displaying Object Attributes per Object Properties</h3> | |
<button onclick="showPropertyAttributes()">Show Property Attributes</button><br> | |
<button onclick="showObjectContent()">Show Object Content</button><br> | |
<br> | |
<p id="output">Output: </p> | |
<p id="propOutput">Properties : </p> | |
<p id="objectContent">Object Content : </p> | |
<script> | |
"use strict"; | |
var outputPre01 = document.getElementById("output").innerHTML; | |
var outputPre02 = document.getElementById("propOutput").innerHTML; | |
var outputPre03 = document.getElementById("objectContent").innerHTML; | |
var person; // declare person | |
prepare(); // init person | |
// showPropertyAttributes | |
function showPropertyAttributes() { | |
var output = document.getElementById("propOutput"); | |
var outTxt = "<br>"; | |
var propertyNames; // object property array | |
var propId; // object property index in property array | |
var propertyDescriptor; // attribute object for each property | |
var attrNames; // attribute property names array | |
var attrId; // attribute index in attribute property names array | |
var propertyNames = Object.getOwnPropertyNames(person); // object propery array | |
for(propId in propertyNames) { | |
outTxt += propertyNames[propId]; | |
propertyDescriptor = Object.getOwnPropertyDescriptor(person, propertyNames[propId]); | |
attrNames = Object.getOwnPropertyNames(propertyDescriptor); | |
outTxt += "<br>"; | |
for(attrId in attrNames) { | |
outTxt += "..." + (attrNames[attrId] + " : " + propertyDescriptor[ attrNames[attrId] ] ) + "<br>"; | |
} | |
outTxt += "<br>"; | |
} | |
writeToOutput( output, outputPre02 + outTxt ); | |
} | |
// showObjectContent | |
function showObjectContent() { | |
var output = document.getElementById("objectContent"); | |
var outTxt = "<br>"; | |
var x; | |
for(x in person) { | |
outTxt += person[x] + "<br>"; | |
} | |
writeToOutput( output, outputPre03 + outTxt ); | |
} | |
// reset Content | |
function resetContent() { | |
var output = document.getElementById("objectContent"); | |
writeToOutput( output, outputPre03 ); | |
} | |
// prepare | |
function prepare() { | |
var output = document.getElementById("output"); | |
person = { | |
id : "1001", | |
firstName : "Levent", | |
lastName : "Divilioglu", | |
age : 33, | |
birthDate : new Date("1982-01-24").getTime(), | |
eyeColor : "brown", | |
printAll : function() { return JSON.stringify(this) } | |
}; | |
writeToOutput( output, outputPre01 + person.printAll() ); | |
} | |
// writer | |
function writeToOutput(outId, msg) { | |
outId.innerHTML = msg; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment