javascript常量_JavaScript中的常量

javascript常量

Perhaps surprisingly, JavaScript has long lacked support for constants: that is, referenced values that don’t change throughout the execution of your script. For lack of any alternatives, most constants have been assigned to variables:

也许令人惊讶的是, JavaScript长期以来一直缺乏对常量的支持:也就是说,在脚本执行过程中引用的值不会改变。 由于没有其他选择,大多数常量已分配给变量:

var DAYSINWEEK = 7;

This is both dangerous and impractical, since it allows the value of DAYSINWEEK to be changed at any time later in your script. Coders have developed various ways of trying to indicate that a JavaScript variable is actually constant: from writing the name in ALL CAPS (a best practice) to the solutions I will discuss below. Thankfully, the latest version of ECMAScript (the standardized specification that JavaScript is derived from) introduces a true constant:

这既危险又不切实际,因为它允许稍后在脚本中随时更改DAYSINWEEK的值。 编码人员已经开发出各种方法来尝试表明JavaScript变量实际上是常量:从在ALL CAPS中写入名称(一种最佳做法)到下面将要讨论的解决方案。 值得庆幸的是,最新版本的ECMAScript(JavaScript衍生自的标准规范)引入了一个真正的常数:

const DAYSINWEEK = 7;

Thereafter DAYSINWEEK can be referred to like a variable, but can never change its value:

此后, DAYSINWEEK可以像变量一样被引用,但是永远不能更改其值:

console.log(DAYSINWEEK);
> 7
DAYSINWEEK = 8;
> error

Once it is declared (constants must be initialized using const, and follow the same naming rules as variables), the constant’s name is reserved: you can’t name a variable DAYSINWEEK and have a constant with the same name, or vice-versa.

一旦声明了它(常量必须使用const初始化,并遵循与变量相同的命名规则),则保留该常量的名称:您不能将变量DAYSINWEEK命名为具有相同名称的常量,反之亦然。

const has good support in recent browsers: IE11 and Edge, Firefox 31+, Opera 12+, Safari 5.1.7+, iOS 7 and higher, together with Chrome 36+. However, this support comes with some important provisions:

const在最新的浏览器中提供良好的支持:IE11和Edge,Firefox 31 +,Opera 12 +,Safari 5.1.7 +,iOS 7和更高版本以及Chrome 36+。 但是,此支持包含一些重要规定:

  1. Chrome does not throw an error if an overwrite is attempted. The value of the constant won’t be altered by any attempts to change it, but a naive coder might assume that a new value will be applied due to the lack of an error message.

    如果尝试覆盖,Chrome不会抛出错误。 常量的值不会通过任何尝试更改的方式来更改,但是幼稚的编码器可能会假定由于缺少错误消息而将应用新值。

  2. constants are not block-scoped in Webkit. That is, references to constants can “leak” outside their current scope.

    在Webkit中,常量不是块作用域的 。 也就是说,对常量的引用可能会“泄漏”到其当前范围之外。

  3. Firefox 35 and earlier allows you to change the value of const on the fly. This is fixed in Firefox 36+.

    Firefox 35及更早版本允许您即时更改const的值 。 Firefox 36+已修复该问题。

  4. Safari 10’s handling of const will cause a conflict if a const takes the same name as a predefined global variable from your markup. That is, if you have an element with the id of #inMediaRes in your page, you cannot use inMediaRes as the name of a constant… not even to reference the same element. Safari is prioritizing the fact that an id automatically turns into a reference in JavaScript, and is reserving the namespace.

    如果const的名称与标记中预定义的全局变量的名称相同,则Safari 10的const处理将导致冲突 。 也就是说,如果页面中有一个id#inMediaRes的元素, 则不能使用inMediaRes作为常量的名称……甚至不能引用相同的元素。 Safari优先考虑一个事实,即id自动变成JavaScript中的引用,并保留名称空间。

It’s worth noting that Webkit’s issues only occur outside of strict mode (the subject of an upcoming article).

值得注意的是,Webkit的问题仅发生在strict模式之外(即将发表的文章的主题)。

const生产就绪了吗? (Is const Production-Ready?)

The choice to use const in your code will depend on several factors: most significantly, the browser versions of site visitors, since a const declaration will be treated as a script error in browsers such as IE10. If you want to use const in development, but are not yet ready to roll it out into production, you have a few choices:

在代码中使用const的选择取决于几个因素:最重要的是,网站访问者的浏览器版本,因为const声明将被视为IE10之类的浏览器中的脚本错误。 如果要在开发中使用const ,但尚未准备好将其投入生产,则有几种选择:

选项1:使用转译器 (Option 1: Use a transpiler)

Transpilers, as the name suggests, transform your code while compiling it into another language: this this case, from ES6 (the specification in which const appears) to ES5. This allows you to develop in the most recent version of the language, but push to production a version that is compatible in a far wider range of browsers. Addy Osmani has an excellent list of ECMAScript transpilers for your use.

Transpilers,顾名思义, 改变代码,同时编译成另一种语言:这这种情况下,从ES6(规范中const出现)来ES5。 这样一来,您就可以使用该语言的最新版本进行开发,而将其推向生产版本,该版本可以与更广泛的浏览器兼容 。 Addy Osmani提供了许多ECMAScript转译器供您使用。

选项2:将常量定义为对象的属性 (Option 2: Define the constant as an object’s property)

This will make the value read-only, although the method isn’t terribly elegant:

尽管该方法并不十分优雅,但这将使该值成为只读的:

var week = (function() {
	var internal = {
	"days": "7"
	};
	return {
		get: function(name) { return internal[name]; }
	};
})();

console.log(week.get("days"));
> 7

翻译自: https://thenewcode.com/1021/Constants-in-JavaScript

javascript常量

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值