我最近在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还是反之?


当前回答

当涉及到let和const(都是块作用域)之间的决定时,总是首选const,以便在代码中使用清楚。这样,如果您试图重新声明变量,就会得到一个错误。如果没有其他选择,只能重新声明它,只需转换为let。注意,正如Anthony所说,const值不是不可变的(例如,const对象的属性可以发生变化)。

When it comes to var, since ES6 is out, I never used it in production code and can't think of a use case for it. One point that might consider one to use it is JavaScript hoisting - while let and const are not hoisted, var declaration is. Yet, beware that variables declared with var have a function scope, not a block scope («if declared outside any function, they will be globally available throughout the program; if declared within a function, they are only available within the function itself», in HackerRank - Variable Declaration Keywords). You can think of let as the block scoped version of var.

其他回答

它提供了:

常量引用,例如,const x =[]——数组可以被修改,但x不能指向另一个数组;而且 块范围。

在ECMAScript 6/2015中,const和let将一起替换var。参见JavaScript ES6变量声明let和const中的讨论

首先,关于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);

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

var:声明一个变量。值初始化是可选的。

let:声明一个块范围的局部变量。

const:声明一个只读命名常量。

例子:

var a;
a = 1;
a = 2; // Reinitialize possible
var a = 3; // Re-declare
console.log(a); // 3

let b;
b = 5;
b = 6; // Reinitialise possible
// let b = 7; // Redeclare not possible
console.log(b);

// const c;
// c = 9;    // Initialization and declaration at the same place
const c = 9;
// const c = 9; // Redeclare and initialization is not possible
console.log(c); // 9
// NOTE: Constants can be declared with uppercase or lowercase, but a common
// convention is to use all-uppercase letters.

var和let的语义

Var和let是对机器和其他程序员的声明:

我希望这个赋值在执行过程中发生变化。不要依赖于这个赋值的最终值。

使用var和let的含义

Var,并强制其他程序员读取从声明到最终使用的所有中间代码,并在程序执行时推断赋值的值。

它们削弱了ESLint和其他语言服务的机器推理,从而在以后的赋值中正确检测输入错误的变量名,并在内部作用域忘记声明的情况下对外部作用域变量名进行作用域重用。

它们还导致运行时在所有代码路径上运行多次迭代,以检测它们实际上是常量,然后才能优化它们。尽管这比错误检测和开发人员可理解性的问题要小。

何时使用const

如果引用的值在执行过程中没有改变,那么表达程序员意图的正确语法是const。对于对象,改变引用的值意味着指向另一个对象,因为引用是不可变的,而对象不是。

“常量”对象

对于对象引用,指针不能更改为另一个对象,但创建并赋值给const声明的对象是可变的。可以从const引用的数组中添加或删除项,也可以更改const引用对象的属性键。

为了实现不可变对象(同样,这使你的代码更容易为人类和机器推理),你可以object .freeze对象在声明/赋值/创建,像这样:

const Options = Object.freeze(['YES', 'NO'])

freeze确实对性能有影响,但您的代码可能因为其他原因而变慢。你想要侧写它。

你也可以将可变对象封装在状态机中,并返回深度副本作为值(这就是Redux和React状态的工作方式)。有关如何从第一原则构建全局状态的示例,请参见避免在Browser JS中使用可变全局状态。

当var和let是一个很好的匹配

Let和var表示可变状态。在我看来,它们应该只用于建模实际的可变状态。比如“连接是否存在?”

这些最好封装在可测试的状态机中,这些状态机公开了表示“连接的当前状态”(在任何时间点都是常数)的常量值,以及代码的其余部分真正感兴趣的内容。

加上合成副作用和转换数据,编程已经够难了。通过创建带有变量的可变状态将每个函数变成不可测试的状态机只会增加复杂性。

要了解更细致的解释,请参见Shun the Mutant - const的案例。