+ 1
Why is this code turning up 'NaN'?
function person(name, age, Game){ this.name= name; this.age= age; this.favGame= Game; this.Year= BirthYear(); } function BirthYear() { return 2018 - this.age; } var Ed = new person(Ed, 28, "Majora's Mask"); var Ann = new person(Ann, 29, "Breath of the Wild"); document.write(Ed.Year);
2 Answers
0
Put the BirthYear function inside the person function to make it a member.
function person(name, age, Game) {
this.name = name;
this.age = age;
this.favGame = Game;
this.Year = BirthYear();
function BirthYear() {
return parseInt(new Date().getFullYear() - age);
}
}
P.S. Please don't use too many words on question's tags, it's confusing and misleading.
0
The problem is caused by the BirthYear function.
This is because it sits outside the person object.
Specifically it is because this.age == window.age not person.age.
Also use Ipangs Date type basrd solution as this will likely work better.