1

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
    }
  }
}
bradlet
  • 36
  • 3
  • 3
    Yes it's possible, in very much the same way as in an object created from a class. Why did you change the methods to arrow functions, and why did you remove `this`? – Bergi Jul 13 '22 at 17:55
  • This has been asked here countless times for over a decade :) – vsync Jul 13 '22 at 17:55
  • @vsync I can't find a decent duplicate – evolutionxbox Jul 13 '22 at 17:56
  • 1
    @evolutionxbox - I've seen a dup many many years ago.. need to remember... – vsync Jul 13 '22 at 17:57
  • Sorry I edited the question, I forgot the class had a constructor and so I changed the non-class version to a function taking in the same values as the constructor. – bradlet Jul 13 '22 at 18:04
  • @Bergi I just wanted to convert it into a regular old object instead of using classes. I don't have any specific reason why, I just prefer using regular objects instead of classes if possible. – bradlet Jul 13 '22 at 18:05
  • @bradlet - `Class` is used to create instances with shared properties or inherit from other classes and expand upon them. `Object` is used for a single-purpose use case, without any shared-properties capabilities. You don't go around changing Classes to Object just because you can.. each situation has a use-case for one or the other. – vsync Jul 13 '22 at 18:08
  • @bradlet I was not talking about the conversion of `class` to an object literal. I was talking about the conversion of [`foo() { … }` method syntax](https://stackoverflow.com/q/32404617/1048572) to `foo: () => {…}` arrow functions, and the removal of `this.` before the method call. – Bergi Jul 13 '22 at 18:10
  • Not sure how to mark this question as solved, but thank you to whoever linked me to the related question. This is solved easily enough by not using arrow functions, which allows you to use `this` to reference the object itself. – bradlet Jul 13 '22 at 18:11

0 Answers0