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 meaning of JavaScript closure

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

Share

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

This article is a detailed introduction to "what is the meaning of JavaScript closure". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "what is the meaning of JavaScript closure" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of Xiaobian.

1. Lexical domain

Closure is a proprietary property of the programming language Lexical Scoping, distinct from dynamic scoping. That is, the function is called in the "variable domain" of its definition, not in the variable domain at the time of its call.

The initial state of a Javascript function includes not only the function itself but also the domain in which the function definition process is located.

Like most modern programming languages, JavaScript uses lexical scoping. This means that functions are executed using the variable scope that was in effect when they were defined, not the variable scope that is in effect when they are invoked. In order to implement lexical scoping, the internal state of a JavaScript function object must include not only the code of the function but also a reference to the scope in which the function definition appears. This combination of a function object and a scope (a set of variable bindings) in which the function’s variables are resolved is called a closure in the computer science literature.

Consider the following example:

function makeCounter () { let counter = 0; return function() {return counter++;}; } let counter = makeCounter(); console.log(counter()); console.log(counter()); console.log(counter()); #+RESULTS: : 0 : 1 : 2

The most interesting thing about this nested function is that when the outer function is called back (here makeCounter()), there is no way to touch the counter variable. Only embedded functions have exclusive access to this variable.

Standard Definition of Closure

Developers should generally know the generic programming term "closure."

Closure means that an inner function always has access to variables and parameters declared in its outer function, even after its outer function is returned (end-of-life). In some programming languages, this is not possible, or functions should be written in a special way to achieve it. But as mentioned above, in JavaScript all functions are inherently closed (with one exception, which is covered in the "new Function" syntax).

That is: Functions in JavaScript automatically remember where they were created through the hidden [[Environment]] attribute, so they all have access to external variables.

Read here, this article "What is the meaning of JavaScript closure" article has been introduced, want to master the knowledge points of this article also need to practice to understand, if you want to know more related content of the article, welcome to pay attention to 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

Internet Technology

Wechat

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

12
Report