当前位置:网站首页>闭包(二)
闭包(二)
2022-07-31 05:10:00 【F班的小夏同学】
闭包这个神奇的东西,现在的定义已经多到大家不知道怎么理解了,今天我们梳理一下各种版本的定义。
古老定义
闭包,是指函数变量可以保存在函数作用域内,因此看起来是函数将变量包裹起来了。
那这样说来,包含变量的函数就是闭包。
//按照古老定义,包含变量的函数就是闭包。
function foo(){
var n=0;
}
console.log(n)//Uncaught ReferenceError: n is not defined
定义一
闭包是指可以访问其所在作用域的函数。
那这样说来,需要通过作用域链查找变量的函数就是闭包。
//按照定义一的说法,需要通过作用域链在全局环境中查找变量n的函数foo()就是闭包
var n = 0;
function foo() {
console.log(n)//0
}
foo();
定义二
闭包是指有权访问另一个函数作用域中的变量的函数
那这样说来,访问上层函数的作用域的内层函数就是闭包。
//按照定义二的说法,嵌套在foo函数里的bar函数就是闭包
function foo(){
var a = 2;
function bar(){
console.log(a); // 2
}
bar();
}
foo();
定义三
闭包是指在函数声明时的作用域以外的地方被调用的函数。
在函数声明时的作用域以外的地方调用函数,需要通过将该函数作为返回值或者作为参数被传递。
- 返回值
//按照定义三的说法,在foo()函数的作用域中声明,在全局环境的作用域中被调用的bar()函数是闭包
function foo(){
var a=2;
function bar(){
console.log(a)//2
}
return bar;
}
foo()();
可以简写成如下表示:
function foo(){
var a=2;
return function (){
console.log(a);//2
}
}
foo()();
- 参数
//按照定义三的说法,在foo()函数的作用域中声明,在bar()函数的作用域中被调用的baz()函数是闭包。
function foo(){
var a=2;
function baz(fn){
console.log(a);//2
}
bar(baz);
}
function bar(fn){
fn();
}
因此,无论通过何种手段,只要将内部函数传递到所在的作用域以外,他都会持有对原始作用域的引用,无论在何处执行这个函数都会使用闭包。
IIFE
IIFE是不是闭包?
foo()函数在全局作用域定义,也在全局作用域被立即调用,如果按照定义一的说法来说,他是闭包。如果按照定义二和定义三的说法,他又不是闭包。
var a=2;
(function foo(){
console.log(a);//2
})()
还有一个重要的原因,在requireJS出现之前,实现模块化编程主要通过IIFE,而在IIFE中常见的操作就是window.fn=fn来暴露接口,而这个fn就是闭包,而IIFE只是另一个包含闭包的函数调用。
(function(){
var a = 0;
function fn(){
console.log(a);
}
window.fn = fn;
})()
fn();
最后:
严格来说, 闭包需要满足三个条件,
- 访问所在作用域
- 函数嵌套
- 在所在作用域外被调用
边栏推荐
- 【一起学Rust】Rust的Hello Rust详细解析
- Redis Advanced - Cache Issues: Consistency, Penetration, Penetration, Avalanche, Pollution, etc.
- Interviewer: If the order is not paid within 30 minutes, it will be automatically canceled. How to do this?
- 工作流编排引擎-Temporal
- Flask-based three-party login process
- wpf ScrowViewer水平滚动
- If the account number or password is entered incorrectly for many times, the account will be banned.
- 实验7 UDP与TCP对比
- a different object with the same identifier value was already associated with the session
- Input length must be multiple of 8 when decrypting with padded cipher
猜你喜欢

Anaconda配置环境指令

有了MVC,为什么还要DDD?

Quickly master concurrent programming --- the basics

About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)

MySQL8--Windows下使用压缩包安装的方法

Unity mobile game performance optimization series: performance tuning for the CPU side

Linux系统安装mysql(rpm方式安装)

Typec手机有线网卡网线转网口转接口快充方案

【MQ我可以讲一个小时】

The interviewer asked me TCP three handshake and four wave, I really
随机推荐
数据库上机实验7 数据库设计
剑指offer专项突击版 ---- 第 6 天
Qt Creator + CMake 运行调试总会自动 build 所有目标
Flink sink ES 写入 ES(带密码)
The TOKEN value of Kubernetes joining the cluster expires
Linux系统安装mysql(rpm方式安装)
质量小议12 -- 以测代评
为什么要用Flink,怎么入门使用Flink?
Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
TOGAF之架构标准规范(一)
MySQL (updating)
目标检测学习笔记
Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
MYSQL下载及安装完整教程
C语言教程(二)-printf及c自带的数据类型
[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade
Temporal线上部署
Redis的初识
Typec手机有线网卡网线转网口转接口快充方案
matlab simulink欠驱动水面船舶航迹自抗扰控制研究