每次变量变化时如何在JavaScript中运行函数? [英] How to run a function in JavaScript every time a variable changes?

查看:94
本文介绍了每次变量变化时如何在JavaScript中运行函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript中是否有类似于侦听变量中的变化的事件?所以当它的值被修改时,事件触发,然后我可以调用一个函数。为了更好地理解上下文,我有一个处理对象数组的html呈现的函数,并且我希望每次修改数组时都会自动调用该函数。

Is there a way in JavaScript to have something like an event that listens to changes in a variable? so when its value is modified the event triggers and then I can call a function. To put this more into context I have a function which handles the html rendering of an array of objects, and I want that function to be called automatically every time the array is modified.

感谢。

推荐答案

使用 object.watch docs ,如果本机不支持在这个实现中:所有浏览器的Object.watch()?

这篇关于每次变量变化时如何在JavaScript中运行函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆