The title is probably confusing but I will use a code snippet so hopefully you can explain what's going on.
My constructor
function Person(name, age){
this.name = name;
this.age = age;
this.yearsToRetire = function(){
console.log(this.age); //undefined
return 65-this.age;
}();
}
var joe = new Person ("joe",26);
console.log(joe.yearsToRetire); //NaN
My question is how come it doesn't know the value of this.age when I have already passed it and it should be 26 at the time of execution of yearsToRetire? Is it possible to achieve the effect of getting the years until retirement as the return value rather than a function to execute?(i.e is there a way that I use joe.yearsToRetire to get 39 rather than joe.yearsToRetire())
Thanks