Javascript中的闭包

本文介绍了JavaScript中闭包的概念,解释了内部函数如何访问外部作用域的变量,并详细阐述了闭包形成的条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://jibbering.com/faq/notes/closures/


原文较多,这里摘一些比较重要的来说明问题。

The simple explanation of a Closure is that ECMAScript allows inner functions; function definitions and function expressions that are inside the function bodes of other functions.

最简单的定义:一个内部函数(这个就不用再解释了吧。。),感觉和java的内部类有点相似。。

所以

those inner functions are allowed access to all of the local variables, parameters and declared inner functions within their outer function(s).

内部函数可以访问外部所有的变量、其他内部函数等所有东西。。

只是这样还不够,还要再满足一个条件才能形成闭包。

A closure is formed when one of those inner functions is made accessible outside of the function in which it was contained

即当这个内部函数在包含它的外部函数之外被调用时,就形成了闭包。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值