+ 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);

24th Sep 2018, 7:09 PM
Ed Testa
Ed Testa - avatar
2 ответов
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.
24th Sep 2018, 8:07 PM
Ipang
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.
24th Sep 2018, 10:35 PM
josh mizzi
josh mizzi - avatar