Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
I don't know why this 'this' method doesn't work?
const list = {
    name: 'Harry Potter',
    birthYear: 1995,

    calcAge: function () {
        this.age = 2002 - this.birthYear;
        return this.age;
    }
};

console.log(list.age);
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You are storing a function, not calling it that's why it should print you undefined. To fix it you should call it with list.calcAge().

Your code works and this.age = 2002 - this.birthYear creates the property you want, you just need to call it before using it.

const list = {
    name: 'Harry Potter',
    birthYear: 1995,

    calcAge: function () {
        this.age = 2002 - this.birthYear;
        return this.age;
    }
};
list.calcAge()
console.log(list.age);
about 4 years ago · Juan Pablo Isaza Report

0

Call your function. list.calcAge() You can print it like this console.log(list.calcAge()) You were close though!!

about 4 years ago · Juan Pablo Isaza Report

0

const list = {
    name: 'Harry Potter',
    birthYear: 1995,

    calcAge() {
        this.age = 2002 - this.birthYear;
        return this.age;
    },
};

console.log(list.calcAge());

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!