保护 Javascript 全局作用域的方法
原始问题:
我正在研究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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您给出的示例没有任何区别,但有一个您尚未利用的区别。在第一个示例中,您可以利用变量而不污染全局名称空间或对象名称空间。相当于大多数面向对象语言中的私有字段。您可以这样做:
上面的代码返回
undefined
的原因是因为PrivateVariable
不是question
中的字段。但是question
中的函数可以访问PrivateVariable
。这确实是一个私有变量。另一方面,如果您这样写:
在第二种情况下,
PrivateVariable
实际上不是私有的并且可以公开访问。顺便说一句,您通常不会从属于您问题的函数内部引用
question
。相反,您可以使用this
关键字,如下所示:但这仅适用于公共变量。它使发生的事情更加清楚。此外,在某些情况下,它使维护代码变得更加容易,因为如果您更改
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:
The reason the code above returns
undefined
is becausePrivateVariable
is not a field inquestion
. But functions inquestion
can accessPrivateVariable
. This is truly a private variable.On the other hand, if you wrote it like this:
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 thethis
keyword, like so: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.@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 yourquestion.option3.privateVar
in the global