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


当前回答

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

在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);

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

其他回答

个人喜好。你可以使用const,就像你说的,它不会被重新赋值并且是常量。例如,如果你想分配你的生日。你的生日永远不变,所以你可以把它作为一个常数。但你的年龄确实在变化,所以这可能是一个变量。

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

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

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

2017年更新

这个答案仍然受到很多关注。值得注意的是,这个答案是在2014年初发布的,从那时起发生了很多变化。Ecmascript-6支持现在是标准。所有现代浏览器现在都支持const,所以使用它应该非常安全,没有任何问题。


2014年原创答案

尽管有相当不错的浏览器支持,但我现在还是避免使用它。来自MDN关于const的文章:

const的当前实现是mozilla特定的扩展,不是ECMAScript 5的一部分。它在Firefox和Chrome (V8)中得到支持。从Safari 5.1.7和Opera 12.00开始,如果在这些浏览器中使用const定义变量,以后仍然可以更改它的值。Internet Explorer 6-10不支持,但Internet Explorer 11支持。const关键字当前在函数作用域中声明常量(就像用var声明变量一样)。

它接着说:

const将由ECMAScript 6定义,但具有不同的语义。与使用let语句声明的变量类似,使用const声明的常量将被块作用域化。

如果你确实使用了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代替var比较合适? 答:从来没有!

Q:每次声明一个不会被重新赋值的变量时都应该使用它吗? 答:从来没有!就像这将减少资源消耗一样……

问:用var代替const和用var代替const有什么区别吗? 答:是的!使用var是正确的方法!更容易在开发工具和节省创建一个新的文件进行测试。(var不在const的位置- const试图取代var的位置…)

额外A: let也是一样。JavaScript是一种松散的语言——为什么要限制它?