Skip to content

Instantly share code, notes, and snippets.

@bzdgn
Last active January 13, 2016 23:17
Show Gist options
  • Save bzdgn/a4143ce40537fe6c52cd to your computer and use it in GitHub Desktop.
Save bzdgn/a4143ce40537fe6c52cd to your computer and use it in GitHub Desktop.
JavaScript Demo For Using Method As A Function And As A Property
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Object Methods Example #1</h1>
<br>
<h3>Demonstrating method as a function and as a property</h3>
<button onclick="fullName()">Full Name of Person</button><br>
<button onclick="showFullNameFunc()">Show fullName Function</button><br>
<button onclick="printFunction()">printName output of Student</button><br>
<button onclick="showPrintFunction()">Show printName</button><br>
<button onclick="resetAll()">Reset All</button><br>
<br>
<p id="output">Person: </p>
<p id="fullNameId">Person Full Name : </p>
<p id="printFunctionId">Student Print : </p>
<script>
"use strict";
var outputPre01 = document.getElementById("output").innerHTML;
var outputPre02 = document.getElementById("fullNameId").innerHTML;
var outputPre03 = document.getElementById("printFunctionId").innerHTML;
var person; // declare person
var student; // declare student
prepare(); // init person & student
// fullName
function fullName() {
var output = document.getElementById("fullNameId");
var outTxt = "<br>";
outTxt = person.fullName();
writeToOutput( output, outputPre02 + outTxt );
}
// showFullNameFunc
function showFullNameFunc() {
var output = document.getElementById("fullNameId");
var outTxt = "<br>";
outTxt = person.fullName;
writeToOutput( output, outputPre02 + outTxt );
}
// printFunction
function printFunction() {
var output = document.getElementById("printFunctionId");
var outTxt = "<br>";
outTxt = student.printName();
writeToOutput( output, outputPre03 + outTxt );
}
// showPrintFunction
function showPrintFunction() {
var output = document.getElementById("printFunctionId");
var outTxt = "<br>";
outTxt = student.printName;
writeToOutput( output, outputPre03 + outTxt );
}
// reset Content
function resetAll() {
var output1 = document.getElementById("fullNameId");
var output2 = document.getElementById("printFunctionId");
writeToOutput( output1, outputPre02 );
writeToOutput( output2, outputPre03 );
}
// prepare
function prepare() {
preparePerson();
prepareStudent();
}
// prepare person
function preparePerson() {
var output = document.getElementById("output");
person = {
id : "1017",
firstName : "Levent",
lastName : "Divilioglu",
age : 33,
birthDate : new Date("1982-01-24").getTime(),
eyeColor : "brown",
printAll : function() { return JSON.stringify(this) },
fullName : function() { return this.firstName + " " + this.lastName; }
};
writeToOutput( output, outputPre01 + person.printAll() );
}
// prepare student
function prepareStudent() {
var printFunction = person.fullName;
student = {
id : "1017",
firstName : "Levent",
lastName : "Divilioglu",
printName : printFunction
};
}
// 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