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 are the characteristics of closures in js

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is to share with you about the characteristics of closures in js. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Closure is not only a feature of js, but also a major difficulty. Simply put, the so-called closure means that a function can access variables in the external scope of its function.

The three main characteristics of closures are:

1. Function nesting function

2. Internal functions can access variables of external functions.

3. Parameters and variables are not recycled.

For example:

Function test () {var astat1; return function () {alert (a);}} var try=test (); try (); / / pops up the value of a

In this example, the variable an is inaccessible outside the test method, but inside the test method, there is an anonymous function that is returned through return, the variable an in the test scope.

Can be accessed in anonymous functions. And when the test method is executed, the memory occupied by the variable an is not freed so that the nested function can also be accessed.

The purpose of closures is that private variables and methods can be designed through closures.

For example: create a perosn class in java with the private variable name.

Public class Person {private String name='wy'; public Person (val) {name=val;} public void setName (val) {name=val;} public String getName () {return name;}}

Implement functions similar to java creation classes in js:

(function () {var name= "wangyu"; Person=function (val) {name=val;} Person.prototype.setName=function (val) {name=val;} Person.prototype.getName=function () {return name;}}) (); var person1=new Person ("sj"); alert (this.name) / / undefined cannot access alert (person1.getName ()) outside the function scope; / / sj

Name in function cannot be accessed externally because it is within the scope of function. However, you can modify and access the name value by creating person objects and calling person methods, similar to private variables in the java class, which cannot be accessed externally and can only be accessed through class methods.

Look at another example of a private variable:

Var aaa = (function () {var a = 1; function bbb () {axioms; alert (a);} function ccc () {axioms; alert (a);} return {b:bbb, / / json structure c:ccc}) (); alert (aaa.a) / / undefined aaa.b () / / 2 aaa.c () / / 3

Summary:

1. A closure is a function that has access to variables in the scope of another function. The most common way to create a closure is to create another function within a function and access the function's local variables through another function. The disadvantage of closures is that they are resident in memory, which will increase memory usage, and improper use can easily lead to memory leaks.

2, do not worry about what exactly is a closure, in fact, every function you write is a closure, even if it is a global function, when you access the global variables outside the function, it is the embodiment of the closure.

Thank you for reading! This is the end of this article on "what are the characteristics of closures in js?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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