为什么在 JavaScript 中为块范围变量声明选择名称“let”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37916940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:53:00  来源:igfitidea点击:

Why was the name 'let' chosen for block-scoped variable declarations in JavaScript?

javascriptecmascript-6let

提问by Vitaly Zdanevich

I understand why vartakes that name - it is variable, const- it is a constant, but what is the meaning behind the name for let, which scopes to the current block? Let it be?

我明白为什么要var取这个名字——它是可变的,const——它是一个常数,但名称背后的含义是什么let,它的作用域是当前块?随它去?

回答by exebook

Let is a mathematical statement that was adopted by early programming languages like Scheme and Basic. Variables are considered low level entities not suitable for higher levels of abstraction, thus the desire of many language designers to introduce similar but more powerful concepts like in Clojure, F#, Scala, where letmight mean a value, or a variable that can be assigned, but not changed, which in turn lets the compiler catch more programming errors and optimize code better.

Let 是早期编程语言(如 Scheme 和 Basic)采用的数学语句。变量被认为不适合更高级别的抽象,因此许多语言设计者希望引入类似但更强大的概念,例如在 Clojure、F#、Scala 中,其中let可能意味着一个值或一个可以赋值变量,但没有改变,这反过来又让编译器捕捉到更多的编程错误并更好地优化代码。

JavaScript has had varfrom the beginning, so they just needed another keyword, and just borrowed from dozens of other languages that use letalready as a traditional keyword as close to varas possible, although in JavaScript letcreates block scope local variable instead.

JavaScriptvar从一开始就有,所以他们只需要另一个关键字,并且只是借用了许多其他语言,这些语言let已经尽可能地作为传统关键字使用var,尽管在 JavaScript 中let创建了块作用域局部变量。

回答by Tigran Saluev

I guess it follows mathematical tradition. In mathematics, it is often said "let x be arbitrary real number" or like that.

我想它遵循数学传统。在数学中,常说“让 x 为任意实数”或类似的说法。

回答by Evan White

Adding to exebook's response, the mathematics usage of the keyword letalso encapsulates well the scoping implications of letwhen used in Javascript/ES6. Specifically, just as the following ES6 code is not awareof the assignment in braces of toPrintwhenit prints out the value of 'Hello World',

除了exebook 的响应之外,关键字let的数学用法也很好地封装了let在 Javascript/ES6 中使用时的范围含义。具体来说,就像下面的 ES6 代码在打印出 的值不知道大括号中的赋值一样,toPrint'Hello World'

let toPrint = 'Hello World.';
{
    let toPrint = 'Goodbye World.';
}
console.log(toPrint); // Prints 'Hello World'

letas used in formalized mathematics (especially the writing of proofs) indicates that the current instance of a variable exists only for the scope of that logical idea. In the following example, x immediately gains a new identity upon entering the new idea (usually these are concepts necessary to prove the main idea) and reverts immediately to the old x upon the conclusion of the sub-proof. Of course, just as in coding, this is considered somewhat confusing and so is usually avoided by choosing a different name for the other variable.

let用于形式化数学(尤其是证明的编写)表明变量的当前实例仅存在于该逻辑思想的范围内。在下面的例子中,x 在输入新想法时立即获得一个新身份(通常这些是证明主要想法所必需的概念),并在子证明结束时立即恢复到旧 x。当然,就像在编码中一样,这被认为有些混乱,因此通常可以通过为另一个变量选择不同的名称来避免。

Let x be so and so...

让 x 某某...

  Proof stuff

  证明材料

 New Idea { Let x be something else ... prove something } Conclude New Idea

 新想法 { 让 x 成为别的东西......证明某事} 总结新想法

 Prove main idea with old x

 用旧 x 证明主要思想

回答by Charlie

It does exactly what the vardoes with a scope difference. Now it can not take the name varsince that is already taken.

var与作用域差异完全相同。现在它不能使用这个名字,var因为它已经被占用了。

So it looks that it has taken the next best name which has a semantic in an interesting English language construct.

所以看起来它采用了下一个最好的名字,它在有趣的英语语言结构中具有语义。

let myPet = 'dog';

In English it says "Let my pet be a dog"

用英语说“让我的宠物成为一只狗”

回答by oxalorg

The most likely possibility is that it was the most idiomatic choice. Not only is it easy to speak, but rather intuitive to understand. Some could argue, even more so than var.

最有可能的是,这是最惯用的选择。不仅说起来容易,而且理解起来也很直观。有些人可能会争论,甚至比var.

But I reckon there's a little more history to this.

但我认为这有更多的历史。

From Wikipedia:

来自维基百科

Dana Scott's LCF language was a stage in the evolution of lambda calculus into modern functional languages. This language introduced the let expression, which has appeared in most functional languages since that time.

State-full imperative languages such as ALGOL and Pascal essentially implement a let expression, to implement restricted scope of functions, in block structures.

Dana Scott 的 LCF 语言是 lambda 演算演变为现代函数式语言的一个阶段。这种语言引入了 let 表达式,从那时起,它就出现在大多数函数式语言中。

全状态命令式语言,如 ALGOL 和 Pascal,本质上实现了一个 let 表达式,以在块结构中实现有限范围的函数。

I would like to believe this was an inspiration too, for the letin Javascript.

我想相信这也是一种灵感,对于letin Javascript。

回答by OG Sean

Let uses a more immediate block level limited scope whereas var is function scope or global scope typically.

Let 使用更直接的块级有限范围,而 var 通常是函数范围或全局范围。

It seems let was chosen most likely because it is found in so many other languages to define variables, such as BASIC, and many others.

似乎最有可能选择 let 是因为在许多其他语言中都可以找到它来定义变量,例如 BASIC 和许多其他语言。

回答by Eric H.

I think JavaScript's indebtedness to Scheme is obvious here. Scheme not only has let, but has let*, let*-values, let-syntax, and let-values. (See, The Scheme Programming Language, 4th Ed.).

我认为 JavaScript 对 Scheme 的亏欠是显而易见的。Scheme 不仅有 let,还有 let*、let*-values、let-syntax 和 let-values。(参见,Scheme 编程语言,第 4 版)。

((The choice adds further credence to the notion that JavaScript is Lispy, but--before we get carried away--not homoiconic.))))

((这个选择进一步证实了 JavaScript 是 Lispy 的概念,但是——在我们被带走之前——不是同音异义的。))))

回答by Sebastien Paquet

It could also mean something like "Lexical Environment Type or Tied".. It bothers me that it would simply be "let thisbe that". And let rec wouldn't make sense in lambda calculus.

这也意味着像“词法环境类型或捆绑”。它困扰我,这将仅仅是“让成为那个”。并且 let rec 在 lambda 演算中没有意义。