Skip to content

Instantly share code, notes, and snippets.

@rizalp rizalp/child.js
Last active Dec 23, 2015

Embed
What would you like to do?
//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
You can’t perform that action at this time.