JavaScript中是否有常量?

本文翻译自:Are there constants in JavaScript?

Is there a way to use constants in JavaScript? 有没有办法在JavaScript中使用常量?

If not, what's the common practice for specifying variables that are used as constants? 如果没有,指定用作常量的变量的常见做法是什么?


#1楼

参考:https://stackoom.com/question/XvA/JavaScript中是否有常量


#2楼

I too have had a problem with this. 我也有这个问题。 And after quite a while searching for the answer and looking at all the responses by everybody, I think I've come up with a viable solution to this. 经过一段时间寻找答案并查看每个人的所有回复,我想我已经想出了一个可行的解决方案。

It seems that most of the answers that I've come across is using functions to hold the constants. 似乎我遇到的大部分答案都是使用函数来保存常量。 As many of the users of the MANY forums post about, the functions can be easily over written by users on the client side. 正如许多论坛的许多用户所发布的那样,这些功能可以很容易地被客户端的用户覆盖。 I was intrigued by Keith Evetts' answer that the constants object can not be accessed by the outside, but only from the functions on the inside. 我对Keith Evetts的回答很感兴趣,即外部不能访问常量对象,而只能从内部的函数访问。

So I came up with this solution: 所以我提出了这个解决方案:

Put everything inside an anonymous function so that way, the variables, objects, etc. cannot be changed by the client side. 将所有内容放在匿名函数中,这样客户端就无法更改变量,对象等。 Also hide the 'real' functions by having other functions call the 'real' functions from the inside. 还可以通过让其他函数从内部调用“真实”函数来隐藏“真实”函数。 I also thought of using functions to check if a function has been changed by a user on the client side. 我还想过使用函数来检查客户端用户是否更改了某个函数。 If the functions have been changed, change them back using variables that are 'protected' on the inside and cannot be changed. 如果功能已更改,请使用内部“受保护”的变量将其更改回来,并且无法更改。

/*Tested in: IE 9.0.8; Firefox 14.0.1; Chrome 20.0.1180.60 m; Not Tested in Safari*/

(function(){
  /*The two functions _define and _access are from Keith Evetts 2009 License: LGPL (SETCONST and CONST).
    They're the same just as he did them, the only things I changed are the variable names and the text
    of the error messages.
  */

  //object literal to hold the constants
  var j = {};

  /*Global function _define(String h, mixed m). I named it define to mimic the way PHP 'defines' constants.
    The argument 'h' is the name of the const and has to be a string, 'm' is the value of the const and has
    to exist. If there is already a property with the same name in the object holder, then we throw an error.
    If not, we add the property and set the value to it. This is a 'hidden' function and the user doesn't
    see any of your coding call this function. You call the _makeDef() in your code and that function calls
    this function.    -    You can change the error messages to whatever you want them to say.
  */
  self._define = function(h,m) {
      if (typeof h !== 'string') { throw new Error('I don\'t know what to do.'); }
      if (!m) { throw new Error('I don\'t know what to do.'); }
      else if ((h in j) ) { throw new Error('We have a problem!'); }
      else {
          j[h] = m;
          return true;
    }
  };

  /*Global function _makeDef(String t, mixed y). I named it makeDef because we 'make the define' with this
    function. The argument 't' is the name of the const and doesn't need to be all caps because I set it
    to upper case within the function, 'y' is the value of the value of the const and has to exist. I
    make different variables to make it harder for a user to figure out whats going on. We then call the
    _define function with the two new variables. You call this function in your code to set the constant.
    You can change the error message to whatever you want it to say.
  */
  self._makeDef = function(t, y) {
      if(!y) { throw new Error('I don\'t know what to do.'); return false; }
      q = t.toUpperCase();
      w = y;
      _define(q, w);
  };

  /*Global function _getDef(String s). I named it getDef because we 'get the define' with this function. The
    argument 's' is the name of the const and doesn't need to be all capse because I set it to upper case
    within the function. I make a different variable to make it harder for a user to figure out whats going
    on. The function returns the _access function call. I pass the new variable and the original string
    along to the _access function. I do this because if a user is trying to get the value of something, if
    there is an error the argument doesn't get displayed with upper case in the error message. You call this
    function in your code to get the constant.
  */
  self._getDef = function(s) {
      z = s.toUpperCase();
      return _access(z, s);
  };

  /*Global function _access(String g, String f). I named it access because we 'access' the constant through
    this function. The argument 'g' is the name of the const and its all upper case, 'f' is also the name
    of the const, but its the original string that was passed to the _getDef() function. If there is an
    error, the original string, 'f', is displayed. This makes it harder for a user to figure out how the
    constants are being stored. If there is a property with the same name in the object holder, we return
    the constant value. If not, we check if the 'f' variable exists, if not, set it to the value of 'g' and
    throw an error. This is a 'hidden' function and the user doesn't see any of your coding call this
    function. You call the _getDef() function in your code and that function calls this function.
    You can change the error messages to whatever you want them to say.
  */
  self._access = function(g, f) {
      if (typeof g !== 'string') { throw new Error('I don\'t know what to do.'); }
      if ( g in j ) { return j[g]; }
      else { if(!f) { f = g; } throw new Error('I don\'t know what to do. I have no idea what \''+f+'\' is.'); }
  };

  /*The four variables below are private and cannot be accessed from the outside script except for the
    functions inside this anonymous function. These variables are strings of the four above functions and
    will be used by the all-dreaded eval() function to set them back to their original if any of them should
    be changed by a user trying to hack your code.
  */
  var _define_func_string = "function(h,m) {"+"      if (typeof h !== 'string') { throw new Error('I don\\'t know what to do.'); }"+"      if (!m) { throw new Error('I don\\'t know what to do.'); }"+"      else if ((h in j) ) { throw new Error('We have a problem!'); }"+"      else {"+"          j[h] = m;"+"          return true;"+"    }"+"  }";
  var _makeDef_func_string = "function(t, y) {"+"      if(!y) { throw new Error('I don\\'t know what to do.'); return false; }"+"      q = t.toUpperCase();"+"      w = y;"+"      _define(q, w);"+"  }";
  var _getDef_func_string = "function(s) {"+"      z = s.toUpperCase();"+"      return _access(z, s);"+"  }";
  var _access_func_string = "function(g, f) {"+"      if (typeof g !== 'string') { throw new Error('I don\\'t know what to do.'); }"+"      if ( g in j ) { return j[g]; }"+"      else { if(!f) { f = g; } throw new Error('I don\\'t know what to do. I have no idea what \\''+f+'\\' is.'); }"+"  }";

  /*Global function _doFunctionCheck(String u). I named it doFunctionCheck because we're 'checking the functions'
    The argument 'u' is the name of any of the four above function names you want to check. This function will
    check if a specific line of code is inside a given function. If it is, then we do nothing, if not, then
    we use the eval() function to set the function back to its original coding using the function string
    variables above. This function will also throw an error depending upon the doError variable being set to true
    This is a 'hidden' function and the user doesn't see any of your coding call this function. You call the
    doCodeCheck() function and that function calls this function.    -    You can change the error messages to
    whatever you want them to say.
  */
  self._doFunctionCheck = function(u) {
      var errMsg = 'We have a BIG problem! You\'ve changed my code.';
      var doError = true;
      d = u;
      switch(d.toLowerCase())
      {
           case "_getdef":
               if(_getDef.toString().indexOf("z = s.toUpperCase();") != -1) { /*do nothing*/ }
               else { eval("_getDef = "+_getDef_func_string); if(doError === true) { throw new Error(errMsg); } }
               break;
           case "_makedef":
               if(_makeDef.toString().indexOf("q = t.toUpperCase();") != -1) { /*do nothing*/ }
               else { eval("_makeDef = "+_makeDef_func_string); if(doError === true) { throw new Error(errMsg); } }
               break;
           case "_define":
               if(_define.toString().indexOf("else if((h in j) ) {") != -1) { /*do nothing*/ }
               else { eval("_define = "+_define_func_string); if(doError === true) { throw new Error(errMsg); } }
               break;
           case "_access":
               if(_access.toString().indexOf("else { if(!f) { f = g; }") != -1) { /*do nothing*/ }
               else { eval("_access = "+_access_func_string); if(doError === true) { throw new Error(errMsg); } }
               break;
           default:
                if(doError === true) { throw new Error('I don\'t know what to do.'); }
      }
  };

  /*Global function _doCodeCheck(String v). I named it doCodeCheck because we're 'doing a code check'. The argument
    'v' is the name of one of the first four functions in this script that you want to check. I make a different
    variable to make it harder for a user to figure out whats going on. You call this function in your code to check
    if any of the functions has been changed by the user.
  */
  self._doCodeCheck = function(v) {
      l = v;
      _doFunctionCheck(l);
  };
}())

It also seems that security is really a problem and there is not way to 'hide' you programming from the client side. 似乎安全性确实是一个问题,并且无法从客户端“隐藏”您的编程。 A good idea for me is to compress your code so that it is really hard for anyone, including you, the programmer, to read and understand it. 对我来说一个好主意是压缩你的代码,这样任何人,包括你,程序员,都很难阅读和理解它。 There is a site you can go to: http://javascriptcompressor.com/ . 您可以访问以下网站: http//javascriptcompressor.com/ (This is not my site, don't worry I'm not advertising.) This is a site that will let you compress and obfuscate Javascript code for free. (这不是我的网站,不要担心我不做广告。)这是一个允许您免费压缩和混淆Javascript代码的网站。

  1. Copy all the code in the above script and paste it into the top textarea on the javascriptcompressor.com page. 复制上面脚本中的所有代码并将其粘贴到javascriptcompressor.com页面上的顶部textarea中。
  2. Check the Base62 encode checkbox, check the Shrink Variables checkbox. 选中Base62编码复选框,选中Shrink Variables复选框。
  3. Press the Compress button. 按“压缩”按钮。
  4. Paste and save it all in a .js file and add it to your page in the head of your page. 将其全部粘贴并保存在.js文件中,然后将其添加到页面顶部的页面中。

#3楼

No, not in general. 不,不是一般的。 Firefox implements const but I know IE doesn't. Firefox实现const但我知道IE没有。


@John points to a common naming practice for consts that has been used for years in other languages, I see no reason why you couldn't use that. @John指出了在其他语言中使用多年的竞争的常见命名实践,我认为没有理由不能使用它。 Of course that doesn't mean someone will not write over the variable's value anyway. 当然,这并不意味着有人不会写出变量的值。 :) :)


#4楼

Since ES2015 , JavaScript has a notion of const : ES2015开始 ,JavaScript有一个const的概念:

const MY_CONSTANT = "some-value";

This will work in pretty much all browsers except IE 8, 9 and 10 . 除了IE 8,9和10之外,几乎适用于所有浏览器 Some may also need strict mode enabled. 有些可能还需要启用严格模式

You can use var with conventions like ALL_CAPS to show that certain values should not be modified if you need to support older browsers or are working with legacy code: 您可以将var与ALL_CAPS等约定一起使用,以显示如果您需要支持旧版浏览器或使用旧代码,则不应修改某些值:

var MY_CONSTANT = "some-value";

#5楼

I use const instead of var in my Greasemonkey scripts, but it is because they will run only on Firefox... 我在我的Greasemonkey脚本中使用const而不是var ,但这是因为它们只能在Firefox上运行...
Name convention can be indeed the way to go, too (I do both!). 名称约定确实也是要走的路(我两个都做!)。


#6楼

For a while, I specified "constants" (which still weren't actually constants) in object literals passed through to with() statements. 有一段时间,我在传递给with()语句的对象文字中指定了“常量”(仍然不是实际常量)。 I thought it was so clever. 我觉得它太聪明了。 Here's an example: 这是一个例子:

with ({
    MY_CONST : 'some really important value'
}) {
    alert(MY_CONST);
}

In the past, I also have created a CONST namespace where I would put all of my constants. 在过去,我还创建了一个CONST命名空间,我将放置所有常量。 Again, with the overhead. 再次,与开销。 Sheesh. 啧。

Now, I just do var MY_CONST = 'whatever'; 现在,我只做var MY_CONST = 'whatever'; to KISS . 去吻

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值