0

I'm trying to get the bornYear as the result with the below code but getting NaN as a result.

function person(name, age){
    this.name = name;
    this.age = age;
    this.yearOfBirth = bornYear;
}
function bornYear(){
    return 2020 - this.age;
}
document.write(bornYear());

What I'm missing here?

  • 2
    What is supposed to be `this`? Missing some context – Cid Dec 31 '20 at 13:26
  • By the way, the function `bornYear()` will **only** work today, december the 31st. Ignoring the fact that tomorrow the year will change, Tomorrow, doing 2021 - 36 (my age) will give 1985. I'm born in April 1984 – Cid Dec 31 '20 at 13:30
  • 1
    @Cid You mean it will only work after this year’s birthday, don’t you? This information should also be provided, to get a correct result. – Nikolaus Dec 31 '20 at 13:41
  • First of all, why is Person a function, why not a class? and what is the context of this as @Cid asked? – Abdul-Razak Adam Dec 31 '20 at 13:42
  • @Nikolaus as it's written, it will only work flawlessly the last day of year – Cid Dec 31 '20 at 13:49
  • @Cid yes, you’re right I’ve overlooked the word flawlessly. ;-) – Nikolaus Dec 31 '20 at 14:00

3 Answers3

1

Try this:

function bornYear(){
    return 2020 - parseInt(this.age);
}
cbalakus
  • 620
  • 7
  • 15
1

You did not create an instance of person, and you did not call a property of that instance:

  • bornYear references this, which seems intended to be a person instance, so you must bind this to it somehow.
  • As you defined a property yearOfBirth, it would be appropriate to call that method.

Also, your bornYear function is limited to the year 2020. You should take the current year, using the Date constructor.

Here is how it could work:

function person(name, age){
    this.name = name;
    this.age = age;
    this.yearOfBirth = bornYear.bind(this); // bind this
}
function bornYear(){
    // Use the current year to calculate year of birth
    return new Date().getFullYear() - this.age;
}
// First create an instance, then call the method
document.write(new person("Helen", 18).yearOfBirth());

It is more common to define such a method on the prototype though, and to start the constructor name with a capital (Person).

Also, using document.write is bad practice in this scenario. Use console.log.

The standard way would go like this:

class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
    bornYear() {
        return new Date().getFullYear() - this.age;
    }
}
console.log(new Person("Helen", 18).bornYear());
trincot
  • 317,000
  • 35
  • 244
  • 286
1

You seem to be mixing up object oriented programming with a functional style of programming. If you want to use this like you're doing, you'd have to do something like:

function Person(name, age) {
    this.name = name;
    this.age = age;
    this.yearOfBirth = () => {
        return new Date().getFullYear() - this.age;
    };
}
let newPerson = new Person('Foo', 25);
document.write(newPerson.yearOfBirth());
Brother Woodrow
  • 6,092
  • 3
  • 18
  • 20