[英] ES6 中 的 var、let 和 const 应该如何选择?

2,276 阅读2分钟
原文链接: medium.com
本文已经翻译成中文《[译] ES6 中 的 var、let 和 const 应该如何选择?》,欢迎参加「掘金翻译计划」,翻译优质的技术文章。

Perhaps the most important thing you can learn to be a better coder is to keep things simple. In the context of identifiers, that means that a single identifier should only be used to represent a single concept.

Sometimes it’s tempting to create an identifier to represent some data and then use that identifier as a temporary place to store values in transition from one representation to another.

For instance, you may be after a query string parameter value, and start by storing an entire URL, then just the query string, then the value. This practice should be avoided.

It’s easier to understand if you use one identifier for the URL, a different one for the query string, and finally, an identifier to store the parameter value you were after.

This is why I favor `const` over `let` in ES6. In JavaScript, `const` means that the identifier can’t be reassigned. (Not to be confused with immutable values. Unlike true immutable datatypes such as those produced by Immutable.js and Mori, a `const` object can have properties mutated.)

If I don’t need to reassign, `const` is my default choice over `let` because I want the usage to be as clear as possible in the code.

I use `let` when I need to reassign a variable. Because I use one variable to represent one thing, the use case for `let` tends to be for loops or mathematical algorithms.

I don’t use `var` in ES6. There is value in block scope for loops, but I can’t think of a situation where I’d prefer `var` over `let`.

`const` is a signal that the identifier won’t be reassigned.

`let`, is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it’s defined in, which is not always the entire containing function.

`var` is now the weakest signal available when you define a variable in JavaScript. The variable may or may not be reassigned, and the variable may or may not be used for an entire function, or just for the purpose of a block or loop.

With `let` and `const` in ES6, it’s no longer safe to check for an identifier’s existence using `typeof`:

function foo () {
  typeof bar;
  let bar = ‘baz’;
}

foo(); // ReferenceError: can't access lexical declaration
       // `bar' before initialization

But you’ll be fine because you took my advice from “Programming JavaScript Applications” and you always initialize your identifiers before you try to use them…

If you need to deallocate a value by unsetting it, you may consider `let` over `const`, but if you really need to micro-manage the garbage collector, you should probably watch “Slay’n the Waste Monster”, instead:

Eric Elliott is the author of “Programming JavaScript Applications” (O’Reilly), and “Learn JavaScript Universal App Development with Node, ES6, & React”. He has contributed to software experiences for Adobe Systems, Zumba Fitness, The Wall Street Journal, ESPN, BBC, and top recording artists including Usher, Frank Ocean, Metallica, and many more.

He spends most of his time in the San Francisco Bay Area with the most beautiful woman in the world.