最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

运维笔记admin18浏览0评论

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

在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);
发布评论

评论列表(0)

  1. 暂无评论