本文翻译自: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 textof the error messages.*///object literal to hold the constantsvar 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 hasto 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'tsee any of your coding call this function. You call the _makeDef() in your code and that function callsthis 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 thisfunction. The argument 't' is the name of the const and doesn't need to be all caps because I set itto upper case within the function, 'y' is the value of the value of the const and has to exist. Imake 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. Theargument 's' is the name of the const and doesn't need to be all capse because I set it to upper casewithin the function. I make a different variable to make it harder for a user to figure out whats goingon. The function returns the _access function call. I pass the new variable and the original stringalong to the _access function. I do this because if a user is trying to get the value of something, ifthere is an error the argument doesn't get displayed with upper case in the error message. You call thisfunction 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 throughthis function. The argument 'g' is the name of the const and its all upper case, 'f' is also the nameof the const, but its the original string that was passed to the _getDef() function. If there is anerror, the original string, 'f', is displayed. This makes it harder for a user to figure out how theconstants are being stored. If there is a property with the same name in the object holder, we returnthe constant value. If not, we check if the 'f' variable exists, if not, set it to the value of 'g' andthrow an error. This is a 'hidden' function and the user doesn't see any of your coding call thisfunction. 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 thefunctions inside this anonymous function. These variables are strings of the four above functions andwill be used by the all-dreaded eval() function to set them back to their original if any of them shouldbe 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 willcheck if a specific line of code is inside a given function. If it is, then we do nothing, if not, thenwe use the eval() function to set the function back to its original coding using the function stringvariables above. This function will also throw an error depending upon the doError variable being set to trueThis is a 'hidden' function and the user doesn't see any of your coding call this function. You call thedoCodeCheck() function and that function calls this function.    -    You can change the error messages towhatever 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 differentvariable to make it harder for a user to figure out whats going on. You call this function in your code to checkif 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 . 去吻 。

JavaScript中是否有常量?相关推荐

  1. javascript中特殊数值常量

    特殊数值的常量 Infinity                                表示无穷大的特殊值 NaN                                   特殊的非 ...

  2. JavaScript中的常量:什么时候使用它,有必要吗?

    本文翻译自:Const in JavaScript: when to use it and is it necessary? I've recently come across the const k ...

  3. javascript中的常量

    javascript中的常量 直接po图和代码 <script type="text/javascript"> /*js中一旦被定义就无法再被修改的变量,称之为常量常量 ...

  4. 为什么要在JavaScript中使用静态类型? (使用Flow进行静态打字的4部分入门)

    by Preethi Kasireddy 通过Preethi Kasireddy 为什么要在JavaScript中使用静态类型? (使用Flow进行静态打字的4部分入门) (Why use stati ...

  5. Javascript 中 null、NaN和undefined的区别

    1.类型分析: js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始类型,第5种为引用类型. 代码 var a1; var a2 = tr ...

  6. [译] JavaScript 中的 CSS:基于组件的样式的未来

    本文讲的是[译] JavaScript 中的 CSS:基于组件的样式的未来, 原文地址:CSS in JavaScript: The future of component-based styling ...

  7. 如何在JavaScript中实现堆栈和队列?

    本文翻译自:How do you implement a Stack and a Queue in JavaScript? What is the best way to implement a St ...

  8. JavaScript中的endsWith

    如何在JavaScript中检查字符串是否以特定字符结尾? 示例:我有一个字符串 var str = "mystring#"; 我想知道该字符串是否以#结尾. 我该如何检查? Ja ...

  9. python中数字是常量吗,【python】常量与变量

    1.代码 (1)机器码:由二进制组成,是给计算机看的,计算机可以直接执行. (2)汇编码:由数字.字母和符号组成的代码. 汇编语言书写的代码必须按照计算机的计算流程 (写代码的流程就是模拟计算机运行的 ...

最新文章

  1. Spring Cloud企业微服务分布式云架构技术点整合
  2. 高性能和可扩展的React-Redux
  3. SpringMVC中Controller如何将数据返回
  4. Vue.js 表单输入绑定
  5. Spring IOC源代码具体解释之容器初始化
  6. 从NT向solaris 8移植C/C++应用
  7. boost::intrusive::member_value_traits用法的测试程序
  8. SpringBoot配置图片虚拟映射
  9. python发动机悬置解耦计算-按重心处整车坐标系解耦
  10. 更改tomcat的根目录路径
  11. 成功解决 AttributeError: module ‘neat’ has no attribute ‘Config’解决方式
  12. append()方法_python之append方法容易踩的坑
  13. 网络模型一般是指 OSI 七层参考模型和 TCP/IP 五层参考模型。
  14. linux下 复制文件显示进度 alias cp
  15. python群控模拟安卓系统_安卓群控系统模拟器
  16. SQL Server 2008管理工具
  17. 2014-2015年开发的机器人仿真测试平台
  18. leetcode(力扣) 718. 最长重复子数组 1143. 最长公共子序列 1035. 不相交的线 (动态规划)
  19. pfx证书导入方式,步骤流程
  20. 计算机图形学 使用OpenGL的场景建模

热门文章

  1. java fileutils 文件夹_java文件操作---FileUtils
  2. 僵尸网络启示录:一个病毒的自白(3)
  3. leaflet——入门(一)
  4. 解决console.log打印对象时出现省略号的情况
  5. html运用javascript设计弹窗,javascript弹出窗口实现代码
  6. ajax同步请求与异步请求
  7. 漫画:什么是 “小镇做题家” ?
  8. 在线英语教育机构阿卡索引入法大大,电子合同助力行业降本增效 | 案例
  9. linux centos7 rpm 安装nginx
  10. SKT常见物理性能测试仪器设备档案