Skip to content

Instantly share code, notes, and snippets.

@gskachkov
Last active November 14, 2017 22:32
Show Gist options
  • Save gskachkov/0aa28a552de916d3546c73a925a19bf3 to your computer and use it in GitHub Desktop.
Save gskachkov/0aa28a552de916d3546c73a925a19bf3 to your computer and use it in GitHub Desktop.
const onChange = value => console.log(value);
const proxy = new Proxy({}, {
set: function (target, propertyName, newValue) {
const oldName = Reflect.get(target, propertyName);
onChange({ propertyName, oldName, newValue });
return Reflect.set(target, propertyName, newValue);
},
deleteProperty: function (target, propertyName) {
onChange({ propertyName, oldValue: Reflect.get(target, propertyName) });
return Reflect.deleteProperty(target, propertyName);
}
});
proxy.id = "foo"; // {propertyName: "id", oldName: undefined, newValue: "foo"}
delete proxy.id; // {propertyName: "id", oldValue: "foo"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment