Last active
December 23, 2015 06:09
-
-
Save rizalp/6592374 to your computer and use it in GitHub Desktop.
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
//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