在 Node.js 中观察对象中的变量

Watch a variable in an object in Node.js

Object 中某些变量的更改需要通知我。但是 Object 不是预定义的。我尝试了以下 Watchers

watchjs

watchObject

gawk

在所有这些观察者中,Object 是预定义的。

我需要如下功能。

//Initially the Object will be empty.
var ex1 = {      
};

//Will watch for variable1 in that Object
watch(ex1, "variable1", function(){
    console.log("Variable1 changed");
});

//Changing or Adding a variable1 in Object. Now the above watch should trigger.
ex1 = {
    variable1 : "Hai",
    variable2 : "Hello"
}

任何 Watchers 这样做或任何调整?

在下面发帖,因为它可能对像我这样的人有所帮助。

我通过使用 assign 实现了这一点。

//Initially the Object will be empty.
var ex1 = {      
};

//Will watch for variable1 in that Object
watch(ex1, "variable1", function(){
    console.log("Variable1 changed");
});

//Changing or Adding a variable1 in Object. Now the above watch got triggered.
var ex2 = {
    variable1 : "Hai",
    variable2 : "Hello"
}

Object.assign(ex1, ex2);