Last active
December 18, 2015 00:49
-
-
Save protestContest/5699786 to your computer and use it in GitHub Desktop.
Javascript objects as JSON
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
// object: | |
function Person(yearsOld) { | |
// internal properties | |
var _Person | |
, age = yearsOld | |
, height = 71 | |
, firstName = "Zack" | |
, lastName = "Michener"; | |
// internal methods | |
function fullName() { | |
return firstName + " " + lastName; | |
} | |
/* public methods and properties, | |
* all privileged | |
*/ | |
_Person = { | |
"gender": "male", | |
"sayName": function() { | |
console.log("Hi, my name is " + fullName()); | |
}, | |
"isOlder": function(otherAge) { | |
return age > otherAge; | |
} | |
}; | |
return _Person; | |
} | |
// create one | |
var me = new Person(22); | |
me.sayName(); | |
if(me.isOlder(21)) { | |
// drink beer | |
} | |
console.log(me.age); // undefined - is private |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment