Home Web Front-end JS Tutorial Popular understanding of closures - 5Clay

Popular understanding of closures - 5Clay

Oct 09, 2017 am 09:54 AM
understand

Speaking of closures, it involves the variable scope of the js function, which is divided into local variables and global variables. Variables outside the function can be directly read from inside the function, but variables inside the function cannot be directly read from outside the function.

But sometimes we need to get the local variables of the function, so the closure is generated. The closure is to define another function inside the function, as shown in the following code:

var foo=(function () {

     var a="11";

     return{
         get_a:function () {
             return a;
         },
         new_a:function (newValue) {
             a=newValue;
         }
     }
} ())
 console.log(foo.a) //输出undefined,因为函数外部不能直接访问内部的局部变量
 console.log(foo.get_a())//输出11
 foo.new_a("我是通过闭包改变的值")//调用并且传参
 console.log(foo.get_a()) //输出我是通过闭包改变的值
Copy after login

In this way we can The local variables inside a function are obtained outside it. In the above function, the function inside return is the closure.

The above is the detailed content of Popular understanding of closures - 5Clay. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

In-depth understanding of how to use Linux pipelines In-depth understanding of how to use Linux pipelines Feb 21, 2024 am 09:57 AM

In-depth understanding of the use of Linux pipes In the Linux operating system, pipes are a very useful function that can use the output of one command as the input of another command, thereby conveniently realizing various complex data processing and operations. A deep understanding of how Linux pipes are used is very important for system administrators and developers. This article will introduce the basic concepts of pipelines and show how to use Linux pipelines for data processing and operations through specific code examples. 1. Basic concepts of pipes in Linux

Understand the importance of Go language comments Understand the importance of Go language comments Mar 29, 2024 pm 04:48 PM

Comments are a very important part of Go programming. Comments can help programmers better understand the logic, purpose, and details of the code, thereby improving the readability and maintainability of the code. This article will introduce the importance of comments in the Go language, and combine it with specific code examples to illustrate how comments help code understanding. First, let's look at a simple Go program example: packagemainimport "fmt" funcmain(){/

How to correctly understand value passing in PHP How to correctly understand value passing in PHP Mar 08, 2024 pm 03:30 PM

How to correctly understand the value passing method in PHP PHP is a scripting language widely used in Web development, and the parameter passing methods in PHP mainly include value passing and reference passing. And understanding how values ​​are passed in PHP is crucial to writing efficient code. This article will discuss the value passing method in PHP in detail and use specific code examples to help readers better understand. The basic concept of value passing method is to copy the value of a variable and pass it to a function or method. Operations on the value within the function will not affect it.

In-depth understanding of the strings.Split function in Go language documentation In-depth understanding of the strings.Split function in Go language documentation Nov 04, 2023 pm 01:14 PM

To deeply understand the strings.Split function in the Go language documentation, specific code examples are required. In the Go language, string operations are a very common requirement. Among them, the strings package is a standard package provided by the Go language and provides a wealth of string processing functions. Among them, the strings.Split function is one of the commonly used functions. Its function is to split a string into a string slice according to the specified delimiter. Before we officially dive into the strings.Split function,

Understanding the middleware of ThinkPHP6 Understanding the middleware of ThinkPHP6 Jun 20, 2023 am 10:03 AM

As the complexity of modern web applications continues to increase, code logic is becoming more and more complex. To solve this problem, middleware is becoming more and more popular in modern web development. ThinkPHP6 is a popular PHP framework that also supports middleware. In this article, we will discuss the basics and practical uses of ThinkPHP6 middleware. What is middleware? In web development, middleware refers to a way of processing HTTP requests and responses. When the client sends a request to the server,

Explain in simple terms: Thoroughly understand the working principle of Go language range Explain in simple terms: Thoroughly understand the working principle of Go language range Mar 12, 2024 pm 02:18 PM

Go language is a concise and powerful programming language with unique design and features in many aspects. One of the most impressive features is the range keyword, which is used to iterate over data structures such as arrays, slices, maps, and channels. The flexibility and convenience of range make it easy to traverse complex data structures, but many people are confused about how it works. This article will explain how range works in a simple and in-depth way, and use specific code examples to help readers better understand. First, let's look at a simple example

Familiar with the three working modes of SELinux Familiar with the three working modes of SELinux Feb 26, 2024 pm 03:27 PM

SELinux (Security-EnhancedLinux) is a security module that implements Mandatory Access Control (MAC) in Linux systems. It enforces security policies by applying labels to system objects (files, processes, etc.) for more fine-grained access control. SELinux has three working modes: Enforcing, Permissive and Disabled. This article will introduce these three modes in detail and provide specific code examples. 1

Parsing JS event bubbling: solving common doubts? Parsing JS event bubbling: solving common doubts? Feb 18, 2024 pm 02:16 PM

In-depth understanding of JS event bubbling: What common problems are solved? Event bubbling is an important concept in JavaScript. It refers to when an element triggers an event, because the event will bubble up from the triggering element to its parent element, or even higher-level elements. In web development, understanding the principles and applications of event bubbling can help us solve many common problems. Before formally discussing the application of event bubbling, let’s first understand the basic concept of event bubbling. Here is a simple HTML code example: &l

See all articles