1

Is there a way to be notified of a reference change?

I feel I need to be clear about what I mean:

var a = { aa: 1 };
a = { aa: "allowed" }; // `a`'s reference was modified
a.aa = "foo"; // `a` was mutated, but the reference was not modified. Don't care.

It's that reference change I'm interested in. In Swift this is a didSet block. I'd like to observe changes to the reference that a points to.

Full example:

var reel = [];
function print(message) {
  reel = reel.concat([message]); // non-mutating push changes "reel" reference to this new object.
}

Object.SOME_FUNCTION(reel, onChangeReel); // not a real function

function onChangeReel() { /* update the DOM */ }
SimplGy
  • 18,875
  • 14
  • 95
  • 138
  • I closed this as a duplicate because I believe the linked duplicate covers this, and points to not only new features coming up in ecma 7 but also a shim on github which facilitates the deprecated observe function. – Travis J Aug 11 '16 at 18:13
  • This perhaps would also have served as duplicate question: http://stackoverflow.com/q/28661401/1026459 – Travis J Aug 11 '16 at 18:14
  • @TravisJ but he's not interested in watching/observing/proxying property changes; he's interested in watching variables themselves. –  Aug 12 '16 at 05:40

1 Answers1

0

By "reference change", you seem to mean a change in a variable's value.

No, there is no existing or proposed way to do that.

What can be watched--either with the moribund Object.observe proposal or with proxies, is changes in a property's value.

  • Yeah, maybe that's a clearer description. I didn't want to get wrapped up in reference/value semantics. But yeah, I'm looking for a change watcher/didSet on the value of a `variable`, where variable is defined as an `identifier` in the current `Scope`. Sometimes the answer is "no", and it's great to get a firm no. Sometimes I've been surprised and gotten a reference to a proposal and that's great too. – SimplGy Aug 12 '16 at 23:24