使用 JavaScript 增加局部变量

发布于 2024-11-07 13:15:55 字数 301 浏览 5 评论 0原文

我正在尝试增加一个计数器,但我想将变量移出全局命名空间并在本地声明它。我不知道该怎么做。感谢您的帮助。

这是我目前正在使用的代码。

var mediaClickCounter = 0;
function refreshMediaAds() {
    if (mediaClickCounter < 2) {
        mediaClickCounter++;
    } else {
        mediaClickCounter = 0;
        refreshAds();
    }
}

I am trying to increment a counter, but I would like to move the variable out of the global namespace and declare it locally. I'm not sure how to do this. Thanks for your help.

This is the code I'm using at the moment.

var mediaClickCounter = 0;
function refreshMediaAds() {
    if (mediaClickCounter < 2) {
        mediaClickCounter++;
    } else {
        mediaClickCounter = 0;
        refreshAds();
    }
}

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

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

发布评论

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

评论(6

z祗昰~ 2024-11-14 13:15:55
// the function creates a local scope. 
var refreshMediaAds = (function() { 
    var mediaClickCounter = 0;
    // once executed it returns your actual function.
    return function _refreshMediaAds() {
        if (mediaClickCounter < 2) {
            mediaClickCounter++;
        } else {
            mediaClickCounter = 0;
            refreshAds();
        }
    }
// you execute the function.
})();

关闭<3。

// the function creates a local scope. 
var refreshMediaAds = (function() { 
    var mediaClickCounter = 0;
    // once executed it returns your actual function.
    return function _refreshMediaAds() {
        if (mediaClickCounter < 2) {
            mediaClickCounter++;
        } else {
            mediaClickCounter = 0;
            refreshAds();
        }
    }
// you execute the function.
})();

Closures <3.

南烟 2024-11-14 13:15:55

使用这样的方法怎么样:

var ClickCounter = {
 mediaClickCounter: 0,

 refreshMediaAds:function() {
    if (this.mediaClickCounter < 2) {
        this.mediaClickCounter++;
    } else {
        this.mediaClickCounter = 0;
        refreshAds();
    }
 }
};

然后您可以使用 ClickCounter.refreshMediaAds() 来增加成员变量。

how about using something like this:

var ClickCounter = {
 mediaClickCounter: 0,

 refreshMediaAds:function() {
    if (this.mediaClickCounter < 2) {
        this.mediaClickCounter++;
    } else {
        this.mediaClickCounter = 0;
        refreshAds();
    }
 }
};

and then you can just use ClickCounter.refreshMediaAds() which will increment the member variable.

半夏半凉 2024-11-14 13:15:55

您可以创建一些对象并使用该函数作为其方法是可能的。这样变量将是对象的属性而不是全局变量。

如果您目前没有使用任何框架,那么使用 MooTools 可能会对您有很大帮助。创建和使用对象的一些示例位于此处: http://mootools.net/docs/core/类/类

It is possible, you can create some object and use the function as its method. That way the variable will be object's property and not the global variable.

If you do not use any framework currently, using MooTools may help you a lot. Some examples for creating and using objects are located here: http://mootools.net/docs/core/Class/Class

倚栏听风 2024-11-14 13:15:55

您可以尝试归还您所拥有的(以便您以后可以根据需要使用它)

function refreshMediaAds(mediaClickCounter) {
    if (mediaClickCounter < 2) {
        mediaClickCounter++;
    } else {
        mediaClickCounter = 0;
        refreshAds();
    }
    return mediaClickCounter;
}
//then do: 
refreshMediaAds(clicks)

You can try returning what you have (so you can use it later if you want)

function refreshMediaAds(mediaClickCounter) {
    if (mediaClickCounter < 2) {
        mediaClickCounter++;
    } else {
        mediaClickCounter = 0;
        refreshAds();
    }
    return mediaClickCounter;
}
//then do: 
refreshMediaAds(clicks)
套路撩心 2024-11-14 13:15:55
function refreshMediaAds() {
var mediaClickCounter = 0; // now variable have local scope.
    if (mediaClickCounter < 2) {
        mediaClickCounter++;
    } else {
        mediaClickCounter = 0;
        refreshAds();
    }
}
function refreshMediaAds() {
var mediaClickCounter = 0; // now variable have local scope.
    if (mediaClickCounter < 2) {
        mediaClickCounter++;
    } else {
        mediaClickCounter = 0;
        refreshAds();
    }
}
心房敞 2024-11-14 13:15:55

如果你真的必须这样做,我相信这样的事情会成功。但是,在我看来,您应该将其保留在全局命名空间中。

var refreshMediaAds = function() {
    var mediaClickCounter = 0;
    return function()
    {
        if (mediaClickCounter < 2) {
            mediaClickCounter++;
        } else {
            mediaClickCounter = 0;
            refreshAds();
        }
    }

}();

请注意,我还没有对此进行测试,这是对解决方案的有根据的猜测。

If you really must, I believe something like this would do the trick. However, IMO you should just leave it in the global namespace.

var refreshMediaAds = function() {
    var mediaClickCounter = 0;
    return function()
    {
        if (mediaClickCounter < 2) {
            mediaClickCounter++;
        } else {
            mediaClickCounter = 0;
            refreshAds();
        }
    }

}();

Note, I haven't tested this, it's an educated guess at a solution.

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