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

138
Views
Referencing an object property inside itself

Suppose I have a javascript class like this:

class MyClass {
  constructor (a) {
    this.a = a;
  }

  foo() {
    // do something
  }

  bar() {
    const baz = this.foo();
    // do something with baz
  }
}

In the bar() function I can call the foo() function using this.

Is it possible to do this using an object instead of a class? For example:

const myObj = (a) => {
  return {
    foo: () => {
      // do something
    },
    bar: () => {
      const baz = foo(); // this doesn't work
      const baz = myObj.foo() // this doesn't work either
      // do something with baz
    }
  }
}

A solution I found is to define foo and bar outside of the object and then assign them to the object afterwards, but it doesn't feel right:

const myObj = (a) => {
  const foo = () => {
    // do something
  };
  const bar = () => {
    const baz = foo();
    // do something with baz
  };

  return { foo, bar };
}

Is there a way to reference an object's own property from inside of the object?

Edit: Thanks to the responders, an easy solution is to not use arrow functions, and then you can use this:

const myObj = (a) => {
  return {
    foo: function () {
      // do something
    },
    bar: function () {
      const baz = this.foo() // this works!
      // do something with baz
    }
  }
}
about 4 years ago · Juan Pablo Isaza
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!