网站首页 > 技术文章 正文
分享兴趣,传播快乐,增长见闻,留下美好!亲爱的您,这里是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新学苑原创,如有侵权,请联系删除
文字&排版|橙猫猫
审核|闫庆红
猜你喜欢
- 2025-06-23 JavaScript 开发者常犯的 10 个典型错误
- 2025-06-23 这7道问题可以检验你掌握Javascript的真正实力
- 2025-06-23 HarmonyOS NEXT 如何有效地进行内存管理和避免内存泄露?
- 2025-06-23 JS 函数柯里化(js函数柯里化面试题)
- 2025-06-23 《深入理解javascript原型和闭包系列》 知识点整理
- 2025-06-23 JS面试题突破:什么是闭包?(闭包 js)
- 2025-06-23 JavaScript闭包(javascript 闭包)
- 2025-06-23 “闭包到底是什么?90% 的前端开发者都没真正搞懂!”
- 2024-10-04 两个小案例让你深入理解 Javascript 闭包和作用域
- 2024-10-04 js中的闭包 js闭包的使用
你 发表评论:
欢迎- 560℃Oracle分析函数之Lag和Lead()使用
- 556℃几个Oracle空值处理函数 oracle处理null值的函数
- 546℃Oracle数据库的单、多行函数 oracle执行多个sql语句
- 542℃0497-如何将Kerberos的CDH6.1从Oracle JDK 1.8迁移至OpenJDK 1.8
- 539℃Oracle 12c PDB迁移(一) oracle迁移到oceanbase
- 531℃【数据统计分析】详解Oracle分组函数之CUBE
- 519℃最佳实践 | 提效 47 倍,制造业生产 Oracle 迁移替换
- 510℃Oracle有哪些常见的函数? oracle中常用的函数
- 最近发表
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端react (48)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (54)
- 前端富文本编辑器 (47)
- 前端路由 (61)
- 前端数组 (73)
- 前端排序 (47)
- 前端密码加密 (47)
- Oracle RAC (73)
- oracle恢复 (76)
- oracle 删除表 (48)
- oracle 用户名 (74)
- oracle 工具 (55)
- oracle 内存 (50)
- oracle 导出表 (57)
- oracle 中文 (51)
- oracle的函数 (57)
- 前端调试 (52)
- 前端登录页面 (48)
本文暂时没有评论,来添加一个吧(●'◡'●)