Do you understand JavaScript closures?
When I first encountered the concept of closures in JavaScript, I didn't quite understand it. Now, it's not easy to find a well-explained article just by searching some terminology on Baidu. However, the article by Teacher Ruanyifeng titled "Learning JavaScript Closures" can be considered a good article. I don't want to fully copy this excellent work, as it is not my own understanding, but there are parts that are worth referring to. First, I will quote the last two questions that Teacher Ruanyifeng left for readers at the end of this article.
Question 1
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
return function(){
return this.name;
};
}
};
var f = object.getNameFunc();
console.log(f());
Question 2
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
var that = this;
return function(){
return that.name;
};
}
};
var f = object.getNameFunc();
console.log(f());
Of course, the results of running these two questions can be obtained just by running the code, but as for why the result is like this, we will explain it later. First, let me talk about my understanding of closures.
Closure
The role of a closure can be inferred from its name, suggesting that it involves the concept of scope. Closure itself means "enclosed," and in JavaScript, the variable scope can be said to be enclosed. In addition, variables inside functions are not visible to the outside, and can also be considered a closed environment. So, how can we access the internal variables? This requires an internal function to help, and therefore we can say that closures must be related to this issue.
Let’s look at an example
Code 1
function func(){
var n = 10;
function shown(){
console.log(n);
}
return shown;
}
var f = func();
f();
Code 2
function func(){
var n = 10;
return n;
}
console.log(func());
We see that the execution results of these two pieces of code are the same; both print out the value of n as 10. Although the result is the same, the internal mechanisms of the execution are quite different.
First, let’s look at Code 2. Every time func() runs, the internal variable n will be initialized, memory will be reallocated, and a value will be assigned. When func() finishes running, n will be destroyed. This is the same as in other programming languages when using custom functions.
As for Code 1, the running mechanism is that when func() runs, n is initialized, which is the same as in Code 2. Then, an internal function named shown is created, and this internal function shown keeps the environment of the scope where the function is located. This environment includes any variable created in this scope, and in this case, the variable n. So, every time f() is called, the result of 10 will be printed. However, n does not need to be initialized every time because n is always kept in memory. Of course, this is not a baseless claim. We can view the shown function as an enclosed environment that stores information about n, but this n was created by func, not by shown(). Inside shown(), there is only the address information of n. If shown() were to destroy this information after running, how would it obtain this information the next time it runs? The information is kept in memory, and the n variable remains in memory and has not been destroyed after func() finishes running. If it had been destroyed, when we call f() again, it would print undefined instead of 10. We can verify this by calling f() multiple times, and as expected, the result is always 10.
(I'm not sure if everyone can understand the above paragraph, the wording may be a bit rough, but I think it should still be clear. If there are any questions, feel free to leave a comment for discussion.)
Through the above introduction, we can roughly understand that a closure solves the problem of accessing internal function variables and keeps variables in memory. Both of these are necessary for something to be called a closure. In other words, in the above Code 1, the shown function can be called a closure.
Variable Scope
Since closures involve the issue of accessing internal function variables, variable scope is something we must understand. I wrote an article before about variable scope in JavaScript "Do You Know the Traps Hidden in Variables in JavaScript?", where you can get a clear understanding of variable scope.
After understanding the concept of closures and variable scope, let’s go back to the two questions from the beginning.
Explanation of the First Two Questions
For Question 1, the result is "The Window" because although the object defines a property name, the this object in the return function(){ return this.name; } does not point to the object. This is because during the execution of object.getNameFunc(), the this object was not initialized. The f() function is called by the "global" object, so the information about the this object saved in the closure is not the object’s information. Therefore, this.name does not refer to the object’s name but the global variable name.
For Question 2, the result is "My Object" because the that variable is initialized in getNameFunc(). Since object.getNameFunc() is called by the object, the this in var that = this points to the object. Therefore, that also points to the object, and the information saved in the closure is the object’s information. Therefore, that.name refers to object.name, so the printed result is "My Object".
I wonder if everyone can understand this explanation. There are many different views about closures online. Some say that the outer function is the closure, while others say that the inner function is the closure. The answer in this article leans toward the latter, and I personally think the former is an incorrect view. Whether it is the outer function or the inner function, their role in closures should be the same, and this is definitely not a problem.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to render a boolean value in React JSX
Publish Date:2025/03/08 Views:175 Category:React
-
在 React 中使用 String() 函数在 JSX 中呈现布尔值,例如 h2{字符串(bool1)}/h2 。 默认情况下,布尔值不会在 React 中呈现任何内容,因此我们必须将值转换为字符串才能呈现它。 export defaul
Concatenate JSX elements into an array in React
Publish Date:2025/03/07 Views:123 Category:React
-
在 React 中将 JSX 元素连接成一个数组: 初始化一个空数组。 使用 push() 方法将 JSX 元素推送到数组中。 将最外层 JSX 元素上的 key 属性设置为唯一值。 export default function App () { const fru
Component cannot be used as a JSX component error in React
Publish Date:2025/03/06 Views:136 Category:React
-
出现Component cannot be used as a JSX component的错误有多种原因: 返回 JSX 元素数组而不是单个元素。 从组件返回 JSX 元素或 null 以外的任何值。 具有过时版本的 React 类型。 下面是一个产生上
JSX expressions must have one parent element error in React
Publish Date:2025/03/05 Views:200 Category:React
-
当组件返回多个元素时,出现 React.js 错误JSX 表达式必须有一个父元素。 要解决该错误,请将元素包装在父 div 元素中或使用片段,例如 h2One/h2h2Two/h2/ 。 下面是一个产生上述错误的示例
Expected corresponding JSX closing tag error in React
Publish Date:2025/03/05 Views:121 Category:React
-
当我们忘记关闭 JSX 代码中的标签时,会出现 React.js 错误 Expected corresponding JSX closing tag 。 要解决错误,请使用自闭标签,例如 input / 并确保 JSX 代码中开始和结束标签的顺序是正确的。
Importing JSON files into Vue
Publish Date:2025/02/26 Views:101 Category:Vue
-
Vue.js is a popular JavaScript framework that can quickly build single-page applications (SPA). In Vue.js, we can use JSON files to store data, which is very useful in development. This article will introduce how to introduce JSON files in V
Do you know about the hidden traps in variables in JavaScript?
Publish Date:2025/02/21 Views:183 Category:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
Publish Date:2025/02/21 Views:155 Category:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
将 Pandas DataFrame 转换为 JSON
Publish Date:2024/04/21 Views:154 Category:Python
-
本教程演示了如何将 Pandas DataFrame 转换为 JSON 字符串。