可以在JavaScript中声明全局常量吗? [英] Can global constants be declared in JavaScript?

查看:122
本文介绍了可以在JavaScript中声明全局常量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果是这样,这种声明的语法是什么?

If so, what is the syntax for such a declaration?

推荐答案

Javascript并没有真正的概念命名常量,或对象的不可变属性。 (请注意,我不是在这里讨论ES5。)

Javascript doesn't really have the notion of a named constant, or an immutable property of an object. (Note that I'm not talking about ES5 here.)

您可以使用简单的 var 声明声明全局变量在全局范围内,例如网页包含的脚本中的任何函数外部:

You can declare globals with a simple var declaration in the global scope, like outside any function in a script included by a web page:

<script>
  var EXACTLY_ONE = 1;

那么你的代码当然可以使用那个常量,虽然它不是真正的常量因为值可以被更改(换句话说,更新了属性)。

Then your code can use that constant of course, though it's not really "constant" because the value can be changed (the property updated, in other words).

这篇关于可以在JavaScript中声明全局常量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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