Javascript 中的静态变量仅设置一次
我费了很大的劲才完成这件事……特别是对于 html5 检测脚本。我想要一个仅设置一次且不能再次覆盖的变量。就是这样:
var StaticConfiguration = {};
StaticConfiguration.Main = {
_html5: null
}
StaticConfiguration.getVariable = function(name) {
return StaticConfiguration.Main["_" + name];
}
StaticConfiguration.setVariable = function(name, value) {
if(StaticConfiguration.Main["_" + name] == null) {
StaticConfiguration.Main["_" + name] = value;
}
}
首先,我定义一个包含所有这些变量的全局对象 StaticConfiguration - 在我的例子中,只是“html5”。我将其设置为 null,因为我想在应用程序内设置它。为此,我将其称为
StaticConfiguration.setVariable("html5", "true");
“It's set then”。如果我尝试再次设置它,它会失败 - 当然,因为 _html5 不再为空。所以我实际上使用下划线来“隐藏”静态变量。
这对我有很大帮助。我希望这是一个好方法 - 如果不是请告诉我:)
I was tearing my hair out to get this done...particularly for an html5 detection script. I wanted a variable that is set only once and that can't be overwritten again. This is it:
var StaticConfiguration = {};
StaticConfiguration.Main = {
_html5: null
}
StaticConfiguration.getVariable = function(name) {
return StaticConfiguration.Main["_" + name];
}
StaticConfiguration.setVariable = function(name, value) {
if(StaticConfiguration.Main["_" + name] == null) {
StaticConfiguration.Main["_" + name] = value;
}
}
First, I define a global object StaticConfiguration containing all of these variables - in my case, just "html5". I set it to null, since I want to set it inside the application. To do so, I call
StaticConfiguration.setVariable("html5", "true");
It's set then. If I try to set it again, it fails - of course, since _html5 is not null anymore. So I practically use the underscore to "hide" the static variable.
This is helping me a lot. I hope it's a good approach - please tell me if not :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,它是
true
,而不是"true"
所有 字符串(空字符串除外)的计算结果为 true,包括字符串“假”。
其次,您真的需要这样保护数据吗?无论如何,实际上没有任何方法可以在您的上下文中安全地运行用户的 Javascript。总有办法绕过这样的保护。如果违规代码确实关心的话,它无论如何都可以替换整个
StaticConfiguration
对象。马修的代码是解决该问题的更好方法,但它不遵循单例模式,而是需要实例化的类。如果您想要一个带有“静态”变量的单个对象,我会这样做。
个人旁注:我讨厌“自己的行上的大括号”格式!
First off, it's
true
, not"true"
all strings (apart from the empty string) evaluate to true, including the string"false"
.Second off, do you really need to protect data like this? There's not really any way to safely run a user's Javascript i your context anyway. There's always a way around protection like this. If offending code really cared, it could just replace the whole
StaticConfiguration
object anyway.Matthew's code is a better approach to the problem, but it doesn't follow a singleton pattern, but is a class that needs to be instanciated. I'd do it more like this, if you wanted a single object with "static" variables.
Personal sidenote: I hate the "curly brackets on their own lines" formatting with a passion!
请参阅 Crockford 关于 JavaScript 中的私有成员 的文章。你可以这样做:
Take a look at Crockford's article on Private Members in JavaScript. You can do something like this:
怎么样:
与其他答案类似,但仍然允许任意键。与下划线解决方案不同,这是真正私有的。
How about:
Similar to the other answer, but still allows arbitrary keys. This is truly private, unlike the underscore solution.
我有点好奇为什么你认为你必须做到这种程度才能保护数据不被覆盖。如果您正在检测浏览器,难道不应该只检测一次吗?如果有人用无效数据覆盖它,那么我会认为这将是客户端实现中的问题,而不是库代码中的问题 - 这有意义吗?
顺便说一句,我非常重视 KISS 原则,尤其 当涉及到客户端脚本时。
I'm a little curious as to why you think that you have to go to this extent to protect the data from being overwritten. If you're detecting the browser, shouldn't it only be done once? If someone's overwriting it with invalid data, then I would assume that it would be a problem in the client implementation and not the library code - does that make sense?
As a side note, I'm pretty big on the KISS principle, especially when it comes to client side scripting.
我知道我参加聚会有点晚了,但在这种情况下我通常
I know i'm a little late to the party but in situations like this i usually