shal
shal

Reputation: 3024

Is there a way to create a global setter?

What I need is to have a function, which is called every time an assignments is performed, so for example when there is :

var a = b;
c = d;
// or even 
for(var i=3...){}

I could have a function like :

function assigned(nameL, valL, nameR, valR){
}

I don't have high hopes for that, I also acknowledge that it may speed things down a lot, but I need it only for debugging purposes.

Upvotes: 2

Views: 1288

Answers (2)

Ram
Ram

Reputation: 144689

Is there a way to create a global setter?

No.

ECMAScript2015 introduces Proxy objects which allows you to do "meta programming" but it doesn't work the way you want.

The Proxy object is used to define custom behavior for fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc).

Upvotes: 2

Adam
Adam

Reputation: 1354

Object.defineProperty(window, "varname", {set : callbackfunction}}; is the way to go link

Upvotes: 1

Related Questions