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

What is the scope introduced in es6

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

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "what is the scope introduced in es6", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the scope introduced in es6" can help you solve your doubts.

Block-level scope is introduced in es6; there are only global scope and function scope in es5, and the scope formed by a pair of curly braces in es6 is block-level scope, and functions are explicitly allowed to be declared in block-level scopes. Function declaration statements behave like let and cannot be referenced outside the block-level scope.

This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.

What scope is introduced in es6

ES6 introduces block-level scopes, which explicitly allow functions to be declared in block-level scopes. ES6 states that function declaration statements behave like let in a block-level scope and cannot be referenced outside the block-level scope.

In the ES6 standard, the scope formed by a pair of curly braces is block-level scope.

ES5 states that functions can only be declared in top-level scope and function scope, not in block-level scope.

ES5 has only global scope and function scope, but no block-level scope, which leads to a lot of unreasonable scenarios.

(1) the inner variable may override the outer variable.

Var tmp = new Date (); function f () {console.log (tmp); if (false) {var tmp = 'hello world';}} f (); / / undefined

(2) the loop variables used for counting are leaked into global variables.

Var s = 'hello'; for (var I = 0; I < s. Stories; iDepression +) {console.log (s [I]); / / hello} console.log (I); / / 5

The variable I is only used to control the loop, but after the loop ends, it does not disappear and leaks into a global variable.

Examples are as follows:

/ / ES6 environment of the browser function f () {console.log ('I am outsiders');} (function () {if (false) {/ / repeatedly declare the function f function f () {console.log ('I am insidedness');}} f ();} ()); / / Uncaught TypeError: f is not a function

In the ES6 standard, declaring a function in a block-level scope promotes the function declaration to the top of the block-level scope as a variable declared by var, and only declares the function as a variable, not the function body. The above code is equivalent to:

/ / ES6 environment of the browser function f () {console.log ('I am outsiders');} (function () {var f = undefined; if (false) {function f () {console.log ('I am insights');} f ();} ()) / / Uncaught TypeError: f is not a function has read this article "what is the scope introduced in es6". If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow the industry information channel.

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