我最近在JavaScript中遇到了const关键字。据我所知,它是用来创建不可变变量的,我已经测试了,以确保它不能被重新定义(在Node.js中):

const x = 'const';
const x = 'not-const';

// Will give an error: 'constant 'x' has already been defined'

我意识到它还没有在所有浏览器上标准化——但我只对Node.js V8的环境感兴趣,我注意到某些开发人员/项目似乎非常喜欢它,当var关键字可以用于同样的效果时。

什么时候用const代替var比较合适? 它是否应该被用在每一个不存在的变量上 重新分配是声明的? 如果用var来代替,会有什么不同吗 Const还是反之?


当前回答

'const'指示您的代码标识符将不会被重新分配。

这是一篇关于何时使用“const”,“let”或“var”的好文章:JavaScript ES6+: var, let,或const?

其他回答

简介:

Const创建一个不可变绑定,这意味着Const变量标识符是不可重赋的。

const a = "value1";

你不能用

a = "value2";

然而,如果const标识符包含一个对象或数组,只要不重新赋值,它的值就可以更改。

const x = { a: 1 }

x.a = 2; // Is possible and allowed

const numbers = [1, 2];
numbers.push(3); // Is possible and allowed

请注意,const是块作用域,就像let一样,它与var不同(var是函数作用域)。

简而言之,当某些东西不太可能通过重赋来改变时,使用const,否则使用let或var,这取决于你想要的作用域。

当可以通过重赋改变什么,不能通过重赋改变什么的时候,对代码进行推理就容易得多了。将const对象更改为let对象非常简单。在默认情况下使用const会让您三思而后行。在很多情况下,这是一件好事。

根据我的经验,当我想要设置一些我以后可能想要更改的东西时,我使用const,而不必在代码中寻找已经硬编码的位,例如文件路径或服务器名称。

测试中的错误是另一回事。你试图创建另一个名为x的变量,这将是一个更准确的测试:

const x = 'const';
x = 'not-const';

你的问题有两个方面:使用const而不是var的技术方面是什么,以及这样做与人有关的方面是什么。

技术上的差异是显著的。在编译语言中,常量将在编译时被替换,它的使用将允许其他优化,如删除死代码,以进一步提高代码的运行时效率。最近(使用较少的术语)JavaScript引擎实际上编译JS代码以获得更好的性能,因此使用const关键字将通知它们上述优化是可能的并且应该完成。这将导致更好的性能。

The human-related aspect is about the semantics of the keyword. A variable is a data structure that contains information that is expected to change. A constant is a data structure that contains information that will never change. If there is room for error, var should always be used. However, not all information that never changes in the lifetime of a program needs to be declared with const. If under different circumstances the information should change, use var to indicate that, even if the actual change doesn't appear in your code.

首先,关于const有三个有用的东西(除了它与let共享的作用域改进):

它为以后阅读代码的人记录了该值不能更改。 它可以防止您(或任何跟随您的人)更改值,除非他们返回并有意更改声明。 它可以节省JavaScript引擎在优化方面的一些分析。例如,你已经声明了值不能改变,所以引擎不需要做功来确定值是否改变,所以它可以决定是否基于值不变进行优化。

你的问题:

什么时候用const代替var比较合适?

你可以在任何时候声明一个值永远不变的变量。你认为这是否合适完全取决于你/你的团队的偏好。

每次声明一个不会被重新赋值的变量时都应该使用它吗?

这取决于你/你的团队。

如果使用var代替const '或反之亦然,它是否有任何区别?

Yes:

var and const have different scope rules. (You might have wanted to compare with let rather than var.) Specifically: const and let are block-scoped and, when used at global scope, don't create properties on the global object (even though they do create globals). var has either global scope (when used at global scope) or function scope (even if used in a block), and when used at global scope, creates a property on the global object. See my "three useful things" above, they all apply to this question.

重点在于如何决定在开发过程中应该使用哪一个标识符。

在JavaScript中有三个标识符。

var(可以重新声明和重新初始化) const(不能重新声明和重新初始化,可以使用push更新数组值) Let(可以重新初始化,但不能重新声明)

'var':在编码时,当我们谈论代码标准时,我们通常使用易于其他用户和开发人员理解的标识符的名称。

例如,如果我们正在考虑使用一些输入并处理它并返回一些结果的许多函数,例如:

变量使用的例子

function firstFunction(input1, input2)
{
    var process = input1 + 2;
    var result = process - input2;
    return result;
}


function otherFunction(input1, input2)
{
    var process = input1 + 8;
    var result = process * input2;
    return result;
}

在上面的例子中,两个函数产生不同的-2结果,但使用相同的变量名。在这里,我们可以看到“过程”和“结果”都被用作变量,它们应该是。

常数与变量的例子

const tax = 10;
const pi = 3.1415926535;

function firstFunction(input1, input2)
{
    var process = input1 + 2;
    var result = process - input2;
    result = (result * tax)/100;
    return result;
}


function otherFunction(input1, input2)
{
    var process = input1 + 8;
    var result = process * input2 * pi;
    return result;
}

在JavaScript中使用“let”之前,我们必须在JavaScript文件顶部添加“use strict”

常量和变量的let示例

const tax = 10;
const pi = 3.1415926535;
let trackExecution = '';

function firstFunction(input1, input2)
{
    trackExecution += 'On firstFunction';
    var process = input1 + 2;
    var result = process - input2;
    result = (result * tax)/100;
    return result;
}


function otherFunction(input1, input2)
{
    trackExecution += 'On otherFunction'; # Can add current time
    var process = input1 + 8;
    var result = process * input2 * pi;
    return result;
}

firstFunction();
otherFunction();
console.log(trackExecution);

在上面的例子中,您可以跟踪哪个函数在特定操作期间执行&哪个函数没有使用。