Javascript 中只设置一次的静态变量 [英] Static variable in Javascript that is set only once

查看:70
本文介绍了Javascript 中只设置一次的静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了完成这项工作,我费尽心思……尤其是对于 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;
    }
}

首先,我定义了一个包含所有这些变量的全局对象 StaticConfiguration - 在我的例子中,只是html5".我将它设置为 null,因为我想在应用程序中设置它.为此,我调用

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");

然后就设置好了.如果我尝试再次设置它,它会失败 - 当然,因为 _html5 不再为空.所以我实际上使用下划线来隐藏"静态变量.

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 :)

推荐答案

首先,它是 true,而不是 "true" all 字符串(除了空字符串)评估为真,包括字符串 "false".

First off, it's true, not "true" all strings (apart from the empty string) evaluate to true, including the string "false".

其次,您真的需要像这样保护数据吗?无论如何,没有任何方法可以在您的上下文中安全地运行用户的 Javascript.像这样的保护总是有办法的.如果有问题的代码真的很在意,它无论如何都可以替换整个 StaticConfiguration 对象.

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 的代码是解决问题的更好方法,但它不遵循单例模式,而是一个需要实例化的类.如果您想要一个带有静态"变量的单个对象,我会更像这样.

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.

StaticConfiguration = new (function()
{
  var data = {}
  this.setVariable = function(key, value)
  {
    if(typeof data[key] == 'undefined')
    {
      data[key] = value;
    }
    else
    {
      // Maybe a little error handling too...
      throw new Error("Can't set static variable that's already defined!");
    }
  };

  this.getVariable = function(key)
  {
    if (typeof data[key] == 'undefined')
    {
      // Maybe a little error handling too...
      throw new Error("Can't get static variable that isn't defined!");
    }
    else
    {
      return data[key];
    }
  };
})();

个人旁注:我讨厌自己行的大括号"格式充满热情!

Personal sidenote: I hate the "curly brackets on their own lines" formatting with a passion!

这篇关于Javascript 中只设置一次的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆