SimplGy
SimplGy

Reputation: 20437

Get notified of reference changes in JavaScript

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 */ }

Upvotes: 1

Views: 76

Answers (1)

user663031
user663031

Reputation:

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.

Upvotes: 0

Related Questions