Skip to content

Instantly share code, notes, and snippets.

@antoineMoPa
Last active October 15, 2023 16:56
Show Gist options
  • Save antoineMoPa/8fee45a74f121575ab115de0a4579be3 to your computer and use it in GitHub Desktop.
Save antoineMoPa/8fee45a74f121575ab115de0a4579be3 to your computer and use it in GitHub Desktop.
Javascript debug on property set (debug on property changer)
const object_to_watch = obj; // Change to the object you are inspecting
const property_to_watch = 'example'; // Change to the property name in the object you are inspecting (here obj.example)
// Store the original value
object_to_watch.__storedValue = object_to_watch[property_to_watch];
Object.defineProperty(object_to_watch, property_to_watch, {
get() {
return this.__storedValue;
},
set(newValue) {
// Break when setting the value
// It can be useful to wrap this statement in a if () { } to check for specific values being set.
debugger;
this.__storedValue = newValue;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment