Created
January 14, 2016 15:45
-
-
Save bzdgn/4a2428d7094a719715e3 to your computer and use it in GitHub Desktop.
Adding/Removing Prototypes Demo
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"> | |
<style> | |
.loneButton{ | |
width: 201px; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Object Prototype Example #3</h1> | |
<br> | |
<h3>Adding/Removing Prototypes Demo</h3> | |
<br> | |
<button type="button" class="loneButton" onclick="showObjectContent()">Show Object Content</button><br> | |
<button type="button" class="loneButton" onclick="addNationality()">Add Nationality Property</button><br> | |
<button type="button" class="loneButton" onclick="removeNationality()">Remove Nationality Property</button><br> | |
<button type="button" class="loneButton" onclick="showNationality()">Show Nationality</button><br> | |
<button type="button" class="loneButton" onclick="addFullName()">Add fullName() Method</button><br> | |
<button type="button" class="loneButton" onclick="removeFullName()">Remove fullName() Method</button><br> | |
<button type="button" class="loneButton" onclick="showFullName()">Show Full Name</button><br> | |
<button type="button" class="loneButton" onclick="resetAll()">Reset All</button> | |
<br> | |
<p id="sampleId">Person1 : </p> | |
<p id="sampleId2">Person2 : </p> | |
<p id="outputId">Nationality : </p> | |
<p id="outputId2">Full Name : </p> | |
<p id="contentId">Content #1: </p> | |
<p id="contentId2">Content #2: </p> | |
<script> | |
"use strict"; | |
var outputPre01 = document.getElementById("sampleId").innerHTML; | |
var outputPre02 = document.getElementById("sampleId2").innerHTML; | |
var outputPre03 = document.getElementById("outputId").innerHTML; | |
var outputPre04 = document.getElementById("outputId2").innerHTML; | |
var outputPre05 = document.getElementById("contentId").innerHTML; | |
var outputPre06 = document.getElementById("contentId2").innerHTML; | |
var personObj, personObj2; | |
prepare(); | |
// addNationality | |
function addNationality() { | |
person.prototype.nationality = "Bozdogan"; // default value | |
} | |
// removeNationality | |
function removeNationality() { | |
delete person.prototype.nationality; | |
} | |
// showNationality | |
function showNationality() { | |
var nationality; | |
var outText = ""; | |
var output = document.getElementById("outputId"); | |
nationality = personObj["nationality"]; | |
if( nationality != undefined ) { | |
outText = nationality; | |
} else { | |
outText = "Nationality does not exist in personObj"; | |
} | |
writeToOutput( output, outputPre03 + outText ); | |
} | |
// addFullName | |
function addFullName() { | |
person.prototype.fullName = function() { return this.firstName + ", " + this.lastName; }; | |
} | |
// removeFullName | |
function removeFullName() { | |
delete person.prototype.fullName; | |
} | |
// showFullName | |
function showFullName() { | |
var outText = ""; | |
var output = document.getElementById("outputId2"); | |
if( personObj["fullName"] != undefined ) { | |
outText = personObj["fullName"](); | |
} else { | |
outText = "fullName() does not exist in personObj"; | |
} | |
writeToOutput( output, outputPre04 + outText ); | |
} | |
// showObjectContent | |
function showObjectContent() { | |
var content = document.getElementById("contentId"); | |
var content2 = document.getElementById("contentId2"); | |
var outText = "<br>"; | |
var outText2 = "<br>"; | |
// object #1 | |
var x; | |
for(x in personObj) { | |
outText += x + " : " + personObj[x] + "<br>"; | |
} | |
writeToOutput( content, outputPre04 + outText ); | |
// object #2 | |
for(x in personObj2) { | |
outText2 += x + " : " + personObj2[x] + "<br>"; | |
} | |
writeToOutput( content, outputPre05 + outText ); | |
writeToOutput( content2, outputPre06 + outText2 ); | |
} | |
// prepare | |
function prepare() { | |
var sample = document.getElementById("sampleId"); | |
var sample2 = document.getElementById("sampleId2"); | |
personObj = new person("Levent", "Divilioglu", 33, "kahve"); | |
personObj2 = new person("Ezgi", "Harmanoglu", 25, "ela"); | |
writeToOutput( sample, outputPre01 + JSON.stringify(personObj) ); | |
writeToOutput( sample2, outputPre02 + JSON.stringify(personObj2) ); | |
} | |
// person prototype | |
function person(first, last, age, eye) { | |
this.firstName = first; | |
this.lastName = last; | |
this.age = age; | |
this.eye = eye; | |
} | |
// reset Content | |
function resetAll() { | |
document.getElementById("sampleId").innerHTML = outputPre01; | |
document.getElementById("sampleId2").innerHTML = outputPre02; | |
document.getElementById("outputId").innerHTML = outputPre03; | |
document.getElementById("outputId2").innerHTML = outputPre04; | |
document.getElementById("contentId").innerHTML = outputPre05; | |
document.getElementById("contentId2").innerHTML = outputPre06; | |
// remvoe added properties from the object prototype | |
delete person.prototype.nationality; | |
delete person.prototype.fullName; | |
// initialize with constructors again | |
prepare(); | |
} | |
// 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