I'm currently working on a JavaScript project and I need to be able to dynamically track changes in an object that is passed into my function.
Although I attempted to utilize Proxy objects for this purpose, I couldn't achieve the desired outcome. One approach I considered was referencing Listening for variable changes in JavaScript
Below is a simplified version of the constructor I am using:
function Fluid(options) {
this.options = options;
// Here I want to reactively monitor any changes in 'this.options' and trigger specific code accordingly.
}
let myApp = new Fluid({
testValue: 1
});
The expected functionality would resemble something like this:
function Fluid(options) {
this.options = options;
function doThings() {
if(this.options.testValue === 1) {
console.log("The value is 1");
} else {
console.log("The value is not 1");
}
}
doThings();
// A mechanism to detect changes in the this.options object could be implemented here, triggering the doThings() function upon any modifications.
}
let myApp = new Fluid({
testValue: 1
});
// Console: The value is 1
myApp.testValue = 2;
// Console: The value is not 1