Skip to content

Instantly share code, notes, and snippets.

@protestContest
Last active December 18, 2015 00:49
Show Gist options
  • Save protestContest/5699786 to your computer and use it in GitHub Desktop.
Save protestContest/5699786 to your computer and use it in GitHub Desktop.
Javascript objects as JSON
// 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