我有以下 JavaScript 代码。我想在对象 sizeLogic
中的任何属性发生更改时执行一些逻辑。具体来说,当这些属性中的任何一个设置为 true 时,我想将所有其他属性更改为 false。
问题:我如何实现这一目标?我不确定这在 JavaScript 中是否可能,无需编写像 set_leftTop(x)
这样的方法,以便在方法中我们可以编写自定义逻辑。
var sizeLogic = {
leftTop: true,
leftBottom: false,
rightTop: false,
rightBottom: false
}
最佳答案
您可能会找到object.watch有用取决于您的浏览器支持矩阵。
来自 MDN:
Warning: Generally you should avoid using watch() and unwatch() when possible. These two methods are implemented only in Gecko, and they're intended primarily for debugging use. In addition, using watchpoints has a serious negative impact on performance, which is especially true when used on global objects, such as window. You can usually use setters and getters or proxies instead
关于javascript - 当 JavaScript 属性更改时执行特殊逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32485996/