0

Given an observable like:

import "./styles.css";
import { observable } from "mobx";

const a = observable({
  firstName: {
    value: "a"
  },
  lastName: {
    value: "b",
    foo() {
      // can I access firstName from `this`?
      return this.value;
    }
  }
})

Something like getParent(this).firstName.value?

Stephen Haberman
  • 1,454
  • 1
  • 9
  • 12

1 Answers1

0

You can't but that has nothing to do with mobx. This is just how Javascript works. As you know firstName and lastName are completely separated objects, that happen to be contained in the same parent object. You can't go up in object references.

Ivan V.
  • 7,593
  • 2
  • 36
  • 53
  • I understand for pure JS, yes, but mobx knows the full tree (b/c it's deeply observable) so it very likely could track metadata between levels in the tree. I.e. when it's walking and "observable"-izing each layer, it could/should know "btw this is your parent". – Stephen Haberman May 07 '20 at 13:50