在JavaScript中声明const有什么意义 [英] What's the point of declaring a const in JavaScript

查看:110
本文介绍了在JavaScript中声明const有什么意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为JavaScript中的const变量将是常量……就像无法更改的变量。

因此,我一直认为使用const优于let / var的好处就像减少了一样。资源使用情况。

但是经过快速测试后,它看起来像一个const,几乎就像一个让变量。至少在我的浏览器(Firefox 47.0)中,它是作用域范围内的并且可以修改。

I always thought that const variables in JavaScript would be constants... like "variables that cannot change".
So I always assumed the benefit of using const over let/var would be something like decreased resources usage.
But after a quick test it looked like a const pretty much acts like a let-variable. It's block scoped and can be modified... at least in my browser (Firefox 47.0).

这就是我所做的:

const FOO = [0,1,2]
FOO[0] = 11
console.log(FOO) // > [11, 1, 2]

这导致了我的问题:使用const有什么意义? ?
我的意思是单词更长,很多人不了解const,它看起来像是正常变量。那么,为什么我会给它一个错觉呢?为什么我们不只用let / var的常量写全名呢?

And that leads to my question: What is the point of using const? I mean the word is longer, lot's of people don't know of const and it straight up seems to act like a normal variable. So why would I give the illusion that it is not? Why don't we just keep writing constants as let/var with the name in all-caps?

推荐答案

简而言之,则对象/数组的值不会改变,您只需要更改对象的属性即可。演示:

To put it simply, the value of the object/array doesn't change, you are only mutating the properties of the object. To demonstrate:

const FOO     = {};
FOO.something = 'foo'; // [legal] mutating the object, but still the same object
FOO           = {};    // [illegal] attempt to assign new object

在学习有关对象的知识时,使用对象要简单一些 const 与其他原始类型:

Working with objects is a little less straightforward when learning about const versus other primitive types:

const FOO     = 1;
FOO           = 2;    // [illegal] attempt to assign new value
FOO           = [];   // [illegal] attempt to assign new value






要回答这个问题,不需要 const 。这是一种高级编程,可防止程序员用脚射击自己。


To answer the question, const isn't needed. It's a luxury of programming that prevents programmers from shooting themselves in the foot.

它是一种语法糖,可以帮助确保价值并将想法传达给维护者,但是JavaScript开发人员多年来不用它就可以编程。通过帮助及早发现并防止错误并增强一定的一致性,它只能使语言变得更加完善。

It's syntactic sugar that helps to secure values and communicate ideas to maintainers, but JavaScript developers have been able to program just fine without it for years. It only helps the language become more refined by helping to detect and prevent errors earlier and enforce some consistency.

随着应用程序/模块变得越来越复杂,对解释器/编译器的需求协助变得越来越重要,而 const 在那里可能是一个巨大的好处。

As applications/modules become more complex, the need for interpreter/transpiler assistance becomes that much more important and const can be a huge benefit there.

还值得注意的是,还有其他好处。使用常量时,JavaScript引擎效率更高。尽管性能提升可能是微不足道的,但某些非常密集的应用程序必须释放尽可能多的资源。

Also of note, there are other added benefits. JavaScript engines are more efficient when working with constants. While the performance boost may be marginal, some very intensive applications must free as much resources as they can.

更容易想到为流行变量设置常量:

It's easier to think of setting constants for popular variables:

const PI           = 3.1415926535;
const E            = 2.71828;
const GOLDEN_RATIO = 1.61803398874;
const GRAVITY      = 9.807;

按照惯例,我使用所有大写字母来区分全局变量和常量。

这篇关于在JavaScript中声明const有什么意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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