Scope, or a set of rules that determines where a variable should be located, is one of the most fundamental concepts of any programming language. In fact, it's so basic that we can easily forget how subtle these rules are!
Understanding exactly how the JavaScript engine "thinks" scope will allow you to avoid common mistakes that can result from writing hoisting, prepare you to focus on closures, and get you one step closer to never writing them again again.
...Anyway, it will help you understand lifting and closing.
In this article we will learn about:
- Basic knowledge of scope in JavaScript
- How the interpreter decides which variables belong to which scope
- The actual working principle of hoisting How the
- ES6 keywords
let
andconst
change the game
Let’s dig into it.
If you are interested in learning more about ES6 and how to take advantage of syntax and features to improve and simplify your JavaScript code, why not check out these two courses:
lexical scope
If you've ever written a line of JavaScript before, you'll know that where you define variables determines where you can use > them. The fact that a variable's visibility depends on the structure of the source code is called lexical scope.
There are three ways to create a scope in JavaScript:
- Create function. Variables declared inside a function are only visible inside that function, including within nested functions.
-
Use
let
orconst
Declare variables within a code block. Such declarations are only visible within blocks. - Create catch
"use strict"; var mr_global = "Mr Global"; function foo () { var mrs_local = "Mrs Local"; console.log("I can see " + mr_global + " and " + mrs_local + "."); function bar () { console.log("I can also see " + mr_global + " and " + mrs_local + "."); } } foo(); // Works as expected try { console.log("But /I/ can't see " + mrs_local + "."); } catch (err) { console.log("You just got a " + err + "."); } { let foo = "foo"; const bar = "bar"; console.log("I can use " + foo + bar + " in its block..."); } try { console.log("But not outside of it."); } catch (err) { console.log("You just got another " + err + "."); } // Throws ReferenceError! console.log("Note that " + err + " doesn't exist outside of 'catch'!")
let yet.
Compilation Process: Bird’s Eye View
When you run a piece of JavaScript, two things happen to make it work properly.
- First, compile your source code.
- Then, the compiled code will be executed.
compilation step , the JavaScript engine:
- Write down all variable names
- Register them to the appropriate scope
- Keep space for your own values
execution does the JavaScript engine actually set the value of a variable reference to be equal to its assigned value. Until then, they are undefined.
// I can use first_name anywhere in this program
var first_name = "Peleke";
function popup (first_name) {
// I can only use last_name inside of this function
var last_name = "Sengstacke";
alert(first_name + ' ' + last_name);
}
popup(first_name);
Let's understand the role of the compiler step by step. First, it reads the line
var first_name = "Peleke". Next, it determines the
scope into which the variable is saved. Because we are at the top level of the script, it realizes that we are in global scope. It then saves the variable first_name to the global scope and initializes its value to
undefined.
function popup (first_name). Because the
function keyword is the first thing on the line, it creates a new scope for the function, registers the function's definition with the global scope, and looks inside to find the variable declaration.
var last_name = "Sengstacke" in the first line of our function, the compiler will save the variable
last_name to the scope of <code><code> class=" inline">popup —
not to the global scope — and set its value to undefined.
Note that we haven't actually run anything yet.
The compiler's job at this point is just to make sure it knows everyone's name; it doesn't care what they do.
At this point, our program knows:
- 全局范围内有一个名为
first_name
的变量。 - 全局范围内有一个名为
popup
的函数。 - 在
popup
范围内有一个名为last_name
的变量。 -
first_name
和last_name
的值均为undefined
。
它并不关心我们是否在代码中的其他地方分配了这些变量值。引擎在执行时会处理这个问题。
第 2 步:执行
在下一步中,引擎再次读取我们的代码,但这一次,执行它。
首先,它读取行 var first_name = "Peleke"
。为此,引擎会查找名为 first_name
的变量。由于编译器已经用该名称注册了一个变量,引擎会找到它,并将其值设置为 "Peleke"
。
接下来,它读取行 function popup (first_name)
。由于我们没有在这里执行该函数,因此引擎不感兴趣并跳过它。
最后,它读取行 popup(first_name)
。由于我们在这里执行一个函数,因此引擎:
- 查找
popup
的值 - 查找
first_name
的值 - 将
popup
作为函数执行,并传递first_name
的值作为参数
当执行 popup
时,它会经历相同的过程,但这次是在函数 popup
内。它:
- 查找名为
last_name
的变量 - 将
last_name
的值设置为等于"Sengstacke"
- 查找
alert
,将其作为函数执行,并以"Peleke Sengstacke"
作为参数
事实证明,幕后发生的事情比我们想象的要多得多!
既然您已经了解了 JavaScript 如何读取和运行您编写的代码,我们就准备好解决一些更贴近实际的问题:提升的工作原理。
显微镜下的吊装
让我们从一些代码开始。
bar(); function bar () { if (!foo) { alert(foo + "? This is strange..."); } var foo = "bar"; } broken(); // TypeError! var broken = function () { alert("This alert won't show up!"); }
如果运行此代码,您会注意到三件事:
- 在分配之前,您可以引用
foo
,但其值为undefined
。 - 您可以在定义之前调用
已损坏的
,但您会收到TypeError
。 - 您可以在定义之前调用
bar
,它会按需要工作。
提升是指 JavaScript 使我们声明的所有变量名称在其作用域内的任何地方都可用,包括在我们分配给它们之前 .
代码段中的三种情况是您在自己的代码中需要注意的三种情况,因此我们将一一逐步介绍它们。
提升变量声明
请记住,当 JavaScript 编译器读取像 var foo = "bar"
这样的行时,它会:
- 将名称
foo
注册到最近的范围 - 将
foo
的值设置为未定义
我们可以在赋值之前使用 foo
的原因是,当引擎查找具有该名称的变量时,它确实存在。这就是为什么它不会抛出 ReferenceError
。
相反,它获取值 undefined
,并尝试使用该值执行您要求的任何操作。通常,这是一个错误。
记住这一点,我们可能会想象 JavaScript 在我们的函数 bar
中看到的更像是这样:
function bar () { var foo; // undefined if (!foo) { // !undefined is true, so alert alert(foo + "? This is strange..."); } foo = "bar"; }
如果您愿意的话,这是提升的第一条规则:变量在其整个范围内都可用,但其值为 undefined
,直到您代码分配给他们。
常见的 JavaScript 习惯用法是将所有 var
声明写入其作用域的顶部,而不是首次使用它们的位置。用 Doug Crockford 的话来说,这可以帮助您的代码阅读更像它运行。
仔细想想,这是有道理的。当我们以 JavaScript 读取代码的方式编写代码时,为什么 bar
的行为方式非常清楚,不是吗?那么为什么不一直这样写呢?
提升函数表达式
事实上,当我们在定义之前尝试执行 broken
时,我们得到了 TypeError
,这只是第一条提升规则的一个特例。
我们定义了一个名为 broken
的变量,编译器会在全局范围内注册该变量,并将其设置为等于 undefined
。当我们尝试运行它时,引擎会查找 broken
的值,发现它是 undefined
,并尝试将 undefined
作为函数执行.
显然,undefined
不是一个函数,这就是为什么我们得到 TypeError
!
提升函数声明
最后,回想一下,在定义 bar
之前,我们可以调用它。这是由于第二条提升规则:当 JavaScript 编译器找到函数声明时,它会使其名称和定义在其作用域的顶部可用。再次重写我们的代码:
function bar () { if (!foo) { alert(foo + "? This is strange..."); } var foo = "bar"; } var broken; // undefined bar(); // bar is already defined, executes fine broken(); // Can't execute undefined! broken = function () { alert("This alert won't show up!"); }
同样,当您编写作为JavaScript读取时,它更有意义,您不觉得吗?
查看:
- 变量声明和函数表达式的名称在其整个范围内都可用,但它们的值在赋值之前为
undefined
。 - 函数声明的名称和定义在其整个范围内都可用,甚至在其定义之前。
现在让我们来看看两个工作方式稍有不同的新工具:let
和 const
。
<strong>let</strong>
、<strong></strong>const
和临时死区强>强>
与 var
声明不同,使用 let
和 const
声明的变量不 被编译器提升。
至少,不完全是。
还记得我们如何调用 已损坏的
,但却因为尝试执行 undefined
而收到 TypeError
吗?如果我们使用 let
定义 broken
,我们就会得到 ReferenceError
,而不是:
"use strict"; // You have to "use strict" to try this in Node broken(); // ReferenceError! let broken = function () { alert("This alert won't show up!"); }
当 JavaScript 编译器在第一遍中将变量注册到其作用域时,它对待 let
和 const
的方式与处理 var
的方式不同。
当它找到 var
声明时,我们将该变量的名称注册到其范围,并立即将其值初始化为 undefined
。
但是,使用 let
,编译器会将变量注册到其作用域,但不会初始化其值为 undefined
。相反,它会使变量保持未初始化状态,直到引擎执行您的赋值语句。访问未初始化变量的值会抛出 ReferenceError
,这解释了为什么上面的代码片段在运行时会抛出异常。
let
声明和赋值语句的顶部开头之间的空间称为临时死区。该名称源自以下事实:即使引擎知道名为 foo
的变量(位于 bar
范围的顶部),该变量是“死的”,因为它没有值。
...还因为如果您尝试尽早使用它,它会杀死您的程序。
const
关键字的工作方式与 let
相同,但有两个主要区别:
- 使用
const
声明时,必须分配一个值。 - 您不能为使用
const
声明的变量重新赋值。
这可以保证 const
将始终拥有您最初分配给它的值。
// This is legal const React = require('react'); // This is totally not legal const crypto; crypto = require('crypto');
块范围
let
和 const
与 var
在另一方面有所不同:它们的范围大小。
当您使用 var
声明变量时,它在作用域链的尽可能高的位置可见 - 通常是在最近的函数声明的顶部,或者在全局范围,如果您在顶层声明它。
但是,当您使用 let
或 const
声明变量时,它会尽可能本地可见 -仅 > 在最近的街区内。
块是由大括号分隔的一段代码,如 if
/else
块、for
循环,以及显式“阻止”的代码块,如本段代码所示。
"use strict"; { let foo = "foo"; if (foo) { const bar = "bar"; var foobar = foo + bar; console.log("I can see " + bar + " in this bloc."); } try { console.log("I can see " + foo + " in this block, but not " + bar + "."); } catch (err) { console.log("You got a " + err + "."); } } try { console.log( foo + bar ); // Throws because of 'foo', but both are undefined } catch (err) { console.log( "You just got a " + err + "."); } console.log( foobar ); // Works fine
如果您在块内使用 const
或 let
声明变量,则该变量仅在块内可见,且仅 分配后。
但是,使用 var
声明的变量在尽可能远的地方可见 - 在本例中是在全局范围内。
如果您对 let
和 const
的具体细节感兴趣,请查看 Rauschmayer 博士在《探索 ES6:变量和范围》中对它们的介绍,并查看有关它们的 MDN 文档。
词法 <strong>this</strong>
和箭头函数
从表面上看,this
似乎与范围没有太大关系。事实上,JavaScript 并没有根据我们在这里讨论的范围规则来解析 this
的含义。
至少,通常不会。众所周知,JavaScript 不会根据您使用该关键字的位置来解析 this
关键字的含义:
var foo = { name: 'Foo', languages: ['Spanish', 'French', 'Italian'], speak : function speak () { this.languages.forEach(function(language) { console.log(this.name + " speaks " + language + "."); }) } }; foo.speak();
我们大多数人都认为 this
表示 foo
在 forEach
循环内,因为这就是它在循环之外的含义。换句话说,我们期望 JavaScript 能够解析 this
词法的含义。
但事实并非如此。
相反,它会在您定义的每个函数中创建一个新 this
,并根据您如何调用该函数来决定其含义 -不是您定义它的位置。
第一点类似于在子作用域中重新定义任何变量的情况:
function foo () { var bar = "bar"; function baz () { // Reusing variable names like this is called "shadowing" var bar = "BAR"; console.log(bar); // BAR } baz(); } foo(); // BAR
将 bar
替换为 this
,整个事情应该立即清楚!
传统上,要让 this
按照我们期望的普通旧词法范围变量的方式工作,需要以下两种解决方法之一:
var foo = { name: 'Foo', languages: ['Spanish', 'French', 'Italian'], speak_self : function speak_s () { var self = this; self.languages.forEach(function(language) { console.log(self.name + " speaks " + language + "."); }) }, speak_bound : function speak_b () { this.languages.forEach(function(language) { console.log(this.name + " speaks " + language + "."); }.bind(foo)); // More commonly:.bind(this); } };
在 speak_self
中,我们将 this
的含义保存到变量 self
中,并使用该变量来得到我们想要的参考。在 speak_bound
中,我们使用 bind
来永久将 this
指向给定对象。
ES2015 为我们带来了一种新的选择:箭头函数。
与“普通”函数不同,箭头函数不会通过设置自己的值来隐藏其父作用域的 this
值。相反,他们从词汇上解析其含义。
换句话说,如果您在箭头函数中使用 this
,JavaScript 会像查找任何其他变量一样查找其值。
首先,它检查本地范围内的 this
值。由于箭头函数没有设置一个,因此它不会找到一个。接下来,它检查 this
值的父范围。如果找到,它将使用它。
这让我们可以像这样重写上面的代码:
var foo = { name: 'Foo', languages: ['Spanish', 'French', 'Italian'], speak : function speak () { this.languages.forEach((language) => { console.log(this.name + " speaks " + language + "."); }) } };
如果您想了解有关箭头函数的更多详细信息,请查看 Envato Tuts+ 讲师 Dan Wellman 的有关 JavaScript ES6 基础知识的精彩课程,以及有关箭头函数的 MDN 文档。
结论
到目前为止,我们已经了解了很多内容!在本文中,您了解到:
- 变量在编译期间注册到其作用域,并在执行期间与其赋值相关联。
- 在赋值之前引用使用
let
或 class="inline">const 声明的变量会引发ReferenceError
,并且此类变量是范围到最近的块。 - 箭头函数 允许我们实现
this
的词法绑定,并绕过传统的动态绑定。
您还了解了提升的两条规则:
-
第一条提升规则:函数表达式和
var
声明在其定义的整个范围内都可用,但其值为undefined
直到您的赋值语句执行。 - 第二条提升规则:函数声明的名称及其主体在定义它们的范围内可用。
下一步最好是利用 JavaScript 作用域的新知识来理解闭包。为此,请查看 Kyle Simpson 的 Scopes & Closures。
最后,关于 this
有很多话要说,我无法在此介绍。如果该关键字看起来仍然像是黑魔法,请查看此和对象原型来了解它。
In the meantime, take advantage of what you've learned and make fewer writing mistakes!
Learn JavaScript: The Complete Guide
We've built a complete guide to help you learn JavaScript, whether you're just starting out as a web developer or want to explore more advanced topics.
The above is the detailed content of Understanding Scope in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Yes, WordPress is very suitable for e-commerce. 1) With the WooCommerce plugin, WordPress can quickly become a fully functional online store. 2) Pay attention to performance optimization and security, and regular updates and use of caches and security plug-ins are the key. 3) WordPress provides a wealth of customization options to improve user experience and significantly optimize SEO.

Do you want to connect your website to Yandex Webmaster Tools? Webmaster tools such as Google Search Console, Bing and Yandex can help you optimize your website, monitor traffic, manage robots.txt, check for website errors, and more. In this article, we will share how to add your WordPress website to the Yandex Webmaster Tool to monitor your search engine traffic. What is Yandex? Yandex is a popular search engine based in Russia, similar to Google and Bing. You can excel in Yandex

Do you need to fix HTTP image upload errors in WordPress? This error can be particularly frustrating when you create content in WordPress. This usually happens when you upload images or other files to your CMS using the built-in WordPress media library. In this article, we will show you how to easily fix HTTP image upload errors in WordPress. What is the reason for HTTP errors during WordPress media uploading? When you try to upload files to Wo using WordPress media uploader

Recently, one of our readers reported that the Add Media button on their WordPress site suddenly stopped working. This classic editor problem does not show any errors or warnings, which makes the user unaware why their "Add Media" button does not work. In this article, we will show you how to easily fix the Add Media button in WordPress that doesn't work. What causes WordPress "Add Media" button to stop working? If you are still using the old classic WordPress editor, the Add Media button allows you to insert images, videos, and more into your blog post.

Do you want to know how to use cookies on your WordPress website? Cookies are useful tools for storing temporary information in users’ browsers. You can use this information to enhance the user experience through personalization and behavioral targeting. In this ultimate guide, we will show you how to set, get, and delete WordPresscookies like a professional. Note: This is an advanced tutorial. It requires you to be proficient in HTML, CSS, WordPress websites and PHP. What are cookies? Cookies are created and stored when users visit websites.

Do you see the "429 too many requests" error on your WordPress website? This error message means that the user is sending too many HTTP requests to the server of your website. This error can be very frustrating because it is difficult to find out what causes the error. In this article, we will show you how to easily fix the "WordPress429TooManyRequests" error. What causes too many requests for WordPress429? The most common cause of the "429TooManyRequests" error is that the user, bot, or script attempts to go to the website

WordPresscanhandlelargewebsiteswithcarefulplanningandoptimization.1)Usecachingtoreduceserverload.2)Optimizeyourdatabaseregularly.3)ImplementaCDNtodistributecontent.4)Vetpluginsandthemestoavoidconflicts.5)ConsidermanagedWordPresshostingforenhancedperf

WordPress is very customized, providing a wide range of flexibility and customizability. 1) Through the theme and plug-in ecosystem, 2) use RESTAPI for front-end development, 3) In-depth code level modifications, users can achieve a highly personalized experience. However, customization requires mastering technologies such as PHP, JavaScript, CSS, etc., and pay attention to performance optimization and plug-in selection to avoid potential problems.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
