保护 Javascript 全局作用域的方法

发布于 2024-12-25 12:50:10 字数 1237 浏览 1 评论 0原文

原始问题:

我正在研究js,我想知道这两种保护全局范围的方法之间是否有任何(有用的)区别,除了第二种可以重用(称为再次)。

选项1:

var question = {};
(function(){ 
    question.option1 = function() {
        // some code
    };
})();

选项2:

var question = {};
question.option2 = function () {
    //some code
};
question.option();

谢谢!

编辑1:

谢谢@luisperezphd。你写的和这个有什么区别(除了冗长之外)?

var question = {};
question.option3 = {};
question.option3.privateVar = 0;
question.option3.testing = function () {
    question.option3.privateVar++;
    // some code
};
question.option3.testing();

编辑2:

谢谢兰斯顿和路易斯佩雷斯夫德!我没有意识到 Question.option3.privateVar 在全球范围内可用。

之间有什么区别吗

var question = {};
(function(){
    var privateVar = "some value";
    question.option4 = function(){
        alert(privateVar);
    }
})();
question.option4();

这个:和这个:

var question = {};
question.option5 = function() {
    var privateVar = "some value";
    var someFunction = function() { 
        alert(privateVar);
    }
    return someFunction;
}
question.option5()();

ORIGINAL QUESTION:

i am studying js and i would like to know if there is any (useful) difference between these two ways of protecting the global scope other than the fact that the second one can be reused (called again).

option1:

var question = {};
(function(){ 
    question.option1 = function() {
        // some code
    };
})();

option2:

var question = {};
question.option2 = function () {
    //some code
};
question.option();

thanks!

EDIT 1:

thank you @luisperezphd. is there any difference between what you wrote and this (besides verbosity)?

var question = {};
question.option3 = {};
question.option3.privateVar = 0;
question.option3.testing = function () {
    question.option3.privateVar++;
    // some code
};
question.option3.testing();

EDIT 2:

thank you lanston and luisperezphd! i did not realize that question.option3.privateVar was available in the global.

is there any difference between this:

var question = {};
(function(){
    var privateVar = "some value";
    question.option4 = function(){
        alert(privateVar);
    }
})();
question.option4();

and this:

var question = {};
question.option5 = function() {
    var privateVar = "some value";
    var someFunction = function() { 
        alert(privateVar);
    }
    return someFunction;
}
question.option5()();

?

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

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

发布评论

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

评论(2

小…楫夜泊 2025-01-01 12:50:10

您给出的示例没有任何区别,但有一个您尚未利用的区别。在第一个示例中,您可以利用变量而不污染全局名称空间或对象名称空间。相当于大多数面向对象语言中的私有字段。您可以这样做:

var question = {};
(function(){ 
    var PrivateVariable = 0;
    question.option1 = function() {
        PrivateVariable++;
        // some code
    };
})();

alert(question.PrivateVariable); // returns 'undefined'

上面的代码返回 undefined 的原因是因为 PrivateVariable 不是 question 中的字段。但是question中的函数可以访问PrivateVariable。这确实是一个私有变量。

另一方面,如果您这样写:

var question = {};
question.PrivateVariable = 0;
question.option1 = function() {
    question.PrivateVariable++;
    // some code
};

alert(question.PrivateVariable); // returns 0

在第二种情况下,PrivateVariable 实际上不是私有的并且可以公开访问。

顺便说一句,您通常不会从属于您问题的函数内部引用question。相反,您可以使用 this 关键字,如下所示:

var question = {};
question.PrivateVariable = 0;
question.option1 = function() {
    this.PrivateVariable++;
    // some code
};

但这仅适用于公共变量。它使发生的事情更加清楚。此外,在某些情况下,它使维护代码变得更加容易,因为如果您更改 question 中的变量名称,则无需更改函数内对它的引用。还有其他好处,但我不知道是否应该在这里讨论。

There is no difference in the example you gave, but there is a difference you haven't exploited. In the first example, you can utilize variables and not pollute the global namespace or the object namespace. The equivalent of a private field in most object orientated languages. You would do that like so:

var question = {};
(function(){ 
    var PrivateVariable = 0;
    question.option1 = function() {
        PrivateVariable++;
        // some code
    };
})();

alert(question.PrivateVariable); // returns 'undefined'

The reason the code above returns undefined is because PrivateVariable is not a field in question. But functions in question can access PrivateVariable. This is truly a private variable.

On the other hand, if you wrote it like this:

var question = {};
question.PrivateVariable = 0;
question.option1 = function() {
    question.PrivateVariable++;
    // some code
};

alert(question.PrivateVariable); // returns 0

In this second case, PrivateVariable is in fact not private and publicly accessible.

Incidentally, you normally wouldn't reference question from inside a function that belongs you question. Instead, you would use the this keyword, like so:

var question = {};
question.PrivateVariable = 0;
question.option1 = function() {
    this.PrivateVariable++;
    // some code
};

But this would only work on public variables. It makes it more clear what is going on. Also, in some cases, it makes maintaining the code easier in that if you change the name of the variable from question you wouldn't have to change references to it inside the function. There are other benefits, but I don't know if I should get into it here.

乙白 2025-01-01 12:50:10

@svdsvd,liusperezphd的和你的有很大不同,PrivateVariable,你无法在全局中获取它,但你可以在question.option3.privateVar中获取你的question.option3.privateVar全球的

@svdsvd,It's quite different between liusperezphd's and yours, the PrivateVariable,you can't get it in the global,but you can get your question.option3.privateVar in the global

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