JS 有什么办法可以“绑定”吗?变量状态的函数?

发布于 2024-12-09 05:21:28 字数 159 浏览 0 评论 0 原文

类似于绑定事件,但类似于更改变量。为每个变量创建一个 set() 函数是促进这一点的唯一方法,还是可以将侦听器绑定到变量?

我发现自己在多个函数中围绕变量编写 if 语句,以检查它们的状态。我希望每当该变量发生变化时都会触发一个函数。

(或者这是一个坏主意......)

Similar to binding an event, but rather to the changing of a variable. Is the only way to facilitate this by creating a set() function for each variable, or is it possible to bind a listener to the variable?

I find myself writing if statements around variables throughout several functions, to check their state. I would like to have a function fire whenever that variable changes.

(or is this a Bad Idea...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

始终不够爱げ你 2024-12-16 05:21:28

您可以使用这样的设置器:http://jsfiddle.net/DeyCP/1/

var obj = (function(func) {
    var variable;

    return {
        get something() {
            return variable;
        },

        set something(value) {
            variable = value;
            func();
        }
    };
})(function() {
    alert(123);
});

然后,

obj.something = 'foo'; // alerts 123
obj.something;         // returns foo

You could make use of setters like this: http://jsfiddle.net/DeyCP/1/.

var obj = (function(func) {
    var variable;

    return {
        get something() {
            return variable;
        },

        set something(value) {
            variable = value;
            func();
        }
    };
})(function() {
    alert(123);
});

Then,

obj.something = 'foo'; // alerts 123
obj.something;         // returns foo
谁把谁当真 2024-12-16 05:21:28

您可以使用 Object.prototype 中的 watch 函数。

https://developer.mozilla.org/en/JavaScript/Reference/ Global_Objects/Object/watch

请参阅此答案了解更多信息。

You can use the watch function from the Object.prototype.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/watch

See this answer for more info.

谁的年少不轻狂 2024-12-16 05:21:28

在许多情况下这是可能的,也是一个可行的选择。正如 @John 指出的,您可以在 JavaScript 中监视取消监视对象的属性。然而,这仅适用于 Firefox。相反,我建议您使用 getter 和 setter 来实现您想要的结果,正如 @pimvdb 指出的那样。但请注意,这两种替代方法仅适用于对象的属性(如果将全局变量视为属性,则还适用于全局变量)。您无法在局部变量上获得相同的结果。

It's possible and also a viable option in many circumstances. As @John pointed out, you can watch and unwatch the properties of objects in JavaScript. However, this only works in Firefox. Instead, I suggest you use getters and setters to achieve the result you want as @pimvdb pointed out. Note however that both these alternatives only work on the properties of objects (and global variables if you treat them as properties). You cannot achieve the same result on local variables.

错爱 2024-12-16 05:21:28

我怀疑你想要的东西不能用变量来完成。相反,您可以通过 getter/setter 或属性来使用对象成员来完成此操作。这些概念在面向对象编程中非常常见。以下是一些提示:

I suspect what you want can't be done with variables. Instead, you can do it with object members through getters/setters or properties. These concepts are quite commons in Object Oriented Programming. Here are some pointers:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文