专业编程教程与实战项目分享平台

网站首页 > 技术文章 正文

话说前端补充篇---闭包 什么是闭包 (closure),如何使用它,为什么要使用它?

ins518 2024-10-04 23:58:38 技术文章 13 ℃ 0 评论

分享兴趣,传播快乐,增长见闻,留下美好!亲爱的您,这里是Learning Yard新学苑。今天小编为大家带来“话说前端补充篇---闭包”。

Share interest, spread happiness, increase knowledge, and leave beautiful. Dear, this is the LearingYard Academy! Today, the editor brings the. Welcome to visit!

什么是垃圾回收:垃圾回收机制也称Garbage Collection简称GC。在JavaScript中拥有自动的垃圾回收机制,通过一些回收算法,找出不再使用引用的变量或属性,由JS引擎按照固定时间间隔周期性的释放其所占的内存空间。在C/C++中需要程序员手动完成垃圾回收。

What is Garbage Collection? Garbage collection mechanism is also called garbage collection or GC for short. There is an automatic garbage collection mechanism in JavaScript. Through some recycling algorithms, the variables or attributes that are no longer used are found out, and the JS engine periodically releases the memory space occupied by them at fixed intervals. In C/C++, programmers need to manually complete garbage collection.

垃圾回收机制(GC):为什么需要垃圾回收:程序和人一样,生活时间长了会产生垃圾,程序在运行过程中也会产生垃圾,垃圾积攒过多后,会导致程序运行速度变慢。在JavaScript中的字符串、对象、数组等数据的内存是不固定的,只有真正使用的时候才会动态分配内存。这些数据所占的内存在不使用时,需要进行释放,以便再次使用,否者在可用内存耗尽造成程序崩溃。

Garbage collection mechanism (GC): Why do you need garbage collection? Like people, programs will produce garbage when they live for a long time, and they will also produce garbage when they run. After too much garbage is accumulated, the running speed of the program will slow down. The memory of strings, objects, arrays and other data in JavaScript is not fixed, and the memory will be allocated dynamically only when it is really used. When the memory occupied by these data is not used, it needs to be released for reuse, otherwise the program will crash when the available memory is exhausted.

闭包:闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现。下面就是我的学习笔记,对于Javascript初学者应该是很有用的。是我们必须掌握的js高级特性,那么什么是闭包呢?它又有什么用呢?

Closure: Closure is a difficult and characteristic of Javascript language, and many advanced applications depend on it. The following are my study notes, which should be very useful for beginners of Javascript. Is an advanced feature of js that we must master, so what is a closure? What is the use of it?

我们都知道,js的作用域分两种,全局和局部,基于我们所熟悉的作用域链相关知识,我们知道在js作用域环境中访问变量的权利是由内向外的,内部作用域可以获得当前作用域下的变量并且可以获得当前包含当前作用域的外层作用域下的变量,反之则不能,也就是说在外层作用域下无法获取内层作用域下的变量,同样在不同的函数作用域中也是不能相互访问彼此变量的,那么我们想在一个函数内部也有限权访问另一个函数内部的变量该怎么办呢?闭包就是用来解决这一需求的,闭包的本质就是在一个函数内部创建另一个函数。

As we all know, the scope of js is divided into two types, global and local. Based on our familiar knowledge of scope chain, we know that the right to access variables in the js scope environment is from the inside out, and the internal scope can obtain the variables under the current scope and the variables under the outer scope, and vice versa, that is to say, the variables under the inner scope cannot be obtained under the outer scope. It is also impossible to access each other's variables in different function scopes, so what should we do if we want to have limited access to the variables in another function within one function? Closure is used to solve this requirement, and the essence of closure is to create another function inside one function.

闭包优点:一个是前面提到的可以读取函数内部的变量,另一个就是让这些变量的值始终保持在内存中。

缺点:由于闭包会使得函数中的变量都被保存在内存中,内存消耗很大,所以不能滥用闭包,否则会造成网页的性能问题,在IE中可能导致内存泄露.

Advantages of closure: one is that you can read the variables inside the function mentioned above, and the other is to keep the values of these variables in memory all the time.

Disadvantages: Because the closure will make the variables in the function be stored in memory, and the memory consumption is very high, so the closure cannot be abused, otherwise it will cause performance problems of the web page and may lead to memory leakage in IE.

如果您对今天的文章有独特的想法,欢迎给我们留言,让我们相约明天,祝您今天过得开心快乐。

That's all for today's sharing. If you have a unique idea about the article, please leave us a message, and let us meet tomorrow. I wish you a nice day!

翻译:Google翻译

本文由LearningYard新学苑原创,如有侵权,请联系删除

文字&排版|橙猫猫

审核|闫庆红

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表