为什么块作用域最初没有在 JavaScript 中实现?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17311693/
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-10-27 07:50:22  来源:igfitidea点击:

Why was block scope not originally implemented in JavaScript?

javascriptscope

提问by mricci

I have read, and discovered through my own experience, that JavaScript doesn't have a block scope. Assuming that the language was designed this way for a reason, can you explain to me what is that reason?

我已经阅读并通过我自己的经验发现 JavaScript 没有块作用域。假设语言被设计成这样是有原因的,你能向我解释一下是什么原因吗?

I've looked around on Google and here, but the posts I have found just reiterate that JS has a function scope and not block scope, without explaining why. I'm curious to know why this is actually the case.

我在谷歌和这里环顾四周,但我发现的帖子只是重申 JS 有一个函数作用域而不是块作用域,没有解释原因。我很想知道为什么会这样。

回答by mplungjan

Converting my comment to answer

将我的评论转换为回答

Choice of the creator: I tweeted Brendan and got the following answer:

创建者的选择:我发了推文 Brendan 并得到以下答案

@mplungjan 10 days did not leave time for block scope. Also many "scripting languages" of that mid-90s era had few scopes & grew more later.

@mplungjan 10 天没有为块范围留出时间。此外,90 年代中期的许多“脚本语言”几乎没有范围并且后来发展得更多。



That said, here are some relevant points:

也就是说,这里有一些相关的要点:

Important: JavaScript prior to ECMAScript2015 (6th edition) does not have block scope. Variables introduced within a block are scoped to the containing function or script, and the effects of setting them persist beyond the block itself. In other words, block statements do not introduce a scope. Although "standalone" blocks are valid syntax, you do not want to use standalone blocks in JavaScript, because they don't do what you think they do, if you think they do anything like such blocks in C or Java.

重要提示:ECMAScript2015(第 6 版)之前的 JavaScript 没有块作用域。块中引入的变量的作用域是包含函数或脚本,并且设置它们的效果在块本身之外持续存在。换句话说,块语句不引入作用域。尽管“独立”块是有效的语法,但您不希望在 JavaScript 中使用独立块,因为如果您认为它们在 C 或 Java 中执行此类块,则它们不会执行您认为它们所做的事情。

we can artificially introduce scopes by creating new functions and immediately invoking them

我们可以通过创建新函数并立即调用它们来人为地引入作用域

letand constdeclared variables are hoisted but they are notinitialized to undefinedin the same way varis. Hence, referencing a letor constdeclared variable before its value is assigned raises a ReferenceError.

letconst声明的变量高挂,但他们都没有初始化undefined以同样的方式var是。因此,在赋值之前引用letconst声明的变量会引发 ReferenceError。

Redeclaration of the same variable in the same block scope raises a SyntaxError.

在同一块范围内重新声明同一变量会引发 SyntaxError。

回答by jfriend00

New answer as of 2015. ES6 does have block scope for variable definitions with the letand constkeywords.

截至 2015 年的新答案。ES6 确实具有用于使用letandconst关键字的变量定义的块作用域。

回答by Aadit M Shah

Block scope was not implemented for the following reasons:

由于以下原因,未实现块作用域:

  1. It's makes the language easier to implement. JavaScript was initially designed as a language for writing interactive web applications. Hence it needed to be small and easy to implement.
  2. Block scopes introduce a performance hit to dynamic languages like JavaScript. This is because when you try to access some variable which is not in the current scope JavaScript first checks the current scope, then the parent scope and so on until it either finds the variable or reaches the end. Hence the introduction of block scopes would make variable access in loops and nested loops very slow.
  3. The lack of block scopes makes it easier to write programs. For example say you want to create a variable only if a certain condition is true. All you need to do in JavaScript is declare and define the variable within an ifstatement. In languages like C you would have to declare the variable outside the ifstatement and define it within the ifstatement.
  4. The lack of block scopes allow declarations to be hoisted. This is especially useful in the case of function declarations. For example see this fiddle: http://jsfiddle.net/L6SgM/(note however that this example doesn't work in Firefox).
  5. Since JavaScript supports first-class function expressions we don't need block scopes. They can be simulated using immediately invoked function expressions.
  1. 它使语言更容易实现。JavaScript 最初被设计为一种用于编写交互式 Web 应用程序的语言。因此,它需要体积小且易于实施。
  2. 块作用域会影响 JavaScript 等动态语言的性能。这是因为当您尝试访问某个不在当前作用域内的变量时,JavaScript 首先检查当前作用域,然后是父作用域,依此类推,直到找到该变量或到达末尾。因此,块作用域的引入会使循环和嵌套循环中的变量访问变得非常缓慢。
  3. 缺少块作用域使得编写程序更容易。例如,假设您只想在某个条件为真时创建一个变量。在 JavaScript 中您需要做的就是在if语句中声明和定义变量。在像 C 这样的语言中,您必须在if语句外声明变量并在if语句内定义它。
  4. 缺少块作用域允许提升声明。这在函数声明的情况下特别有用。例如,请参阅此小提琴:http: //jsfiddle.net/L6SgM/(但请注意,此示例在 Firefox 中不起作用)。
  5. 由于 JavaScript 支持一流的函数表达式,因此我们不需要块作用域。可以使用立即调用的函数表达式来模拟它们。

回答by Joseph Myers

There are many reasons, but some that come to mind are to aid in parsing/debugging code that uses object literals (which can sometimes look like a block), and to simplify the garbage collection of local variables.

有很多原因,但我想到的一些原因是帮助解析/调试使用对象文字(有时看起来像块)的代码,并简化局部变量的垃圾收集。

I hope that the promised support (discussed here, for example, http://esdiscuss.org/notes/2012-07-25) turns out to be real because it would be very convenient to use variables like ithat were local to only a single loop.

我希望,承诺支持(这里讨论,例如,http://esdiscuss.org/notes/2012-07-25)被证明是真实的,因为这将是非常方便的使用变量一样i那名当地只有一个单回路。