Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the block-level scope of JavaScript ES's new feature

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use the block-level scope of the new features of JavaScript ES". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the block-level scope of JavaScript ES new features".

1. What is the block-level scope

The so-called block-level scope is that the variable can only be used in the code block or child code block at the time of declaration. There is no block-level scope in versions prior to ECMAScript 2015, but the let keyword provided by ECMAScript 2015 gives JavaScript a block-level scope. The sample code is as follows

/ * * Block-level scopes can only use the let keyword * the let keyword can not only declare block-level scopes, but can also be used in global and functional scopes * / global scopes let a = 100; / / global variables (function () {/ / function scope let b = 200; / / local variables}) () if (true) {/ / block-level scopes let c = 300 / / Local variable} console.log (a); / / 100console.log (b); / / throw exception console.log (c); / / throw exception 2. Why do you need block-level scope

ECMAScript 5 has only global and functional scopes, not block-level scopes. There are some problems with this situation:

Local variables may override global variables

Var v = 100; (function () {console.log (v); / / undefined var v = 200;})

The variable used for counting in the body of the loop is leaked as a global variable

/ / define a loop body for (var v = 0; v < 10; vault +) {console.log ("this is a for loop"); / / this is a for loop * 10} console.log (v); / / 10

If you do not release this variable manually after the loop, its life cycle lives with the script and takes up memory.

3. And function declaration

The ECMAScript5 standard stipulates that the declaration of a function can only be in the global scope and the function scope, not in the block scope.

Situation 1:

If (true) {function f () {}}

Case 2:

Try {function f () {}} catch (e) {/ /...}

The above two functions declare that they are illegal according to the provisions of ECMAScript5.

The ECMAScript 2015 standard states that declaring functions at the block level is similar to the use of the var keyword, that is, it cannot be accessed outside the current block level scope.

{function fun () {console.log ('this is fun');}} fun (); / / the function above is equivalent to the following function {var fn = function () {console.log (' this is fn');}} fn (); / / this is fn// cannot be accessed outside the block-level scope if the let keyword is used {let f = function () {console.log ('this is f') } f (); / / throw exception description information as ReferenceError: f is not defined Thank you for reading, the above is the content of "how to use the new feature block-level scope of JavaScript ES". After the study of this article, I believe you have a deeper understanding of how to use the new feature block-level scope of JavaScript ES, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report