Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kristian Woods
23,414 PointsWhy am I returning the value 'NAN'?
I understand that NAN means 'not a number'. But I can't understand why I'm returning that value.
class Person {
constructor(name) {
this._name = name;
this._age = 28;
}
static addYear(year) {
return this._age += year;
}
get age() {
return this._age;
}
}
Person.addYear(1);
console.log(Person.age);
Thanks
1 Answer
Steven Parker
243,318 PointsThe Person class itself has no "_age" member (but an instance of it would). So when the call is made to addYear, it creates a new variable. But it is unable to establish a value using the addition assignment operator because it had none to begin with. The result of adding 1 to an undefied is "NaN".
Also, I would expect the addYear method to be more useful if it was not a static method, since it would then have access to the instance variable using "this".