Not a whole lot to really say on the topic yet, since I’m still getting into the Object-Oriented side of JavaScript, but I’m loving my newfound knowledge. In the code snippet below, I simply create a function to define the properties of the Person object and extend it using prototype, adding the setAge method.
If you have any comments on a more efficient way to set objects or extend them, I’m all ears. As I mentioned, I just started venturing into O-O JavaScript the last couple of days. Would love to hear any feedback you might have.
function Person(fName, mi, lName, dob) {
this.firstName = fName;
this.mi = mi;
this.lastName = lName;
this.dob = dob;
}
Person.prototype.setAge = function(dob) {
var now = new Date(),
years = (now - dob) / 1000 / 60 / 60 / 24 / 365;
this.age = Math.floor(years);
};
var cory = new Person('Cory', 'M', 'Dorning', new Date(1982,1,18));
cory.setAge(cory.dob);
console.log(cory.age);
NOTE: I left the console.log(...) in for good measure, in case you wanted to play with it for yourself. :cheers:
