Skip to content

Instantly share code, notes, and snippets.

@rizalp
Last active December 23, 2015 06:09
Show Gist options
  • Save rizalp/6592374 to your computer and use it in GitHub Desktop.
Save rizalp/6592374 to your computer and use it in GitHub Desktop.
//this works encapsulating private properties
//now, how about prototypes?
var Child = (function (s, f){
var DOB = new Date();
var sex = s;
var fullName = f;
var mood;
return {
getAge : function(){
var now = new Date();
var age = now.getFullYear() - DOB.getFullYear();
var m = now.getMonth() - DOB.getMonth();
if (m < 0 || (m === 0 && now.getDate() < DOB.getDate())) {
age--;
}
return age;
},
sayHi : function(){
return "Chichi wa saikōdesu! " + fullName + " love you so much!";
},
kissu : function(){
var response = (mood === "happy") ? "ottosan motto, motto, mottttooooo~" :
"Anata wa saiakuda";
return response;
},
getDOB : function(){
return DOB.valueOf();
}
}
});
var ilya = new Child('F', 'Ilyasviel von Einzbern');
var araragi = new Child('M', 'Araragi Koyomi');
console.log(ilya.sayHi());
console.log(ilya.getDOB());
console.log(araragi.sayHi());
console.log(araragi.getDOB());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment