paulirish / break-on-access

break on access to a property
Other
1.16k stars 128 forks source link

Implement with Proxies #14

Open paulirish opened 8 years ago

paulirish commented 8 years ago

ES6 Proxies could be used to pull off the same technique. It perhaps could have a few advantages over using getters.

They're now in most browsers: image

sktguha commented 8 years ago

:+1:

paulirish commented 8 years ago

Any help is appreciated!

ashmind commented 8 years ago

@paulirish Not sure it is possible in general case -- doesn't Proxy have to be a separate object? So it wouldn't help if you want to watch an object already referenced somewhere.

paulirish commented 8 years ago

@ashmind yeah true. the workflow is a little different.

I suppose we could do something like

Object.prototype.debug = function(){

var interceptor = {
  set: function (receiver, property, value) {
    console.trace(property, 'is changed to', value);
    // debugger
    receiver[property] = value;
  }
};

return new Proxy(this, interceptor);
}

which could enable creating the debuggable object at assignment:

this.latLon = app.getLon().debug();

.... oh look at that, somebody already wrote this: check tracePropAccess in http://www.2ality.com/2014/12/es6-proxies.html

paulirish commented 8 years ago

I suppose one benefit is that you can watch any properties, not just specified ones.