当前位置:网站首页>【js基础】闭包的几种情况(代码)
【js基础】闭包的几种情况(代码)
2022-08-08 04:36:00 【想做一只快乐的修狗】
1. 简单写法
function outer() {
let str = '闭包'
function inner() {
console.log(str)
}
inner()
}
outer()
解释: 内层函数inner调用了外层函数outer中的变量str
2. 在函数外部访问函数内部的变量str
function outer() {
let str = '闭包'
function inner() {
console.log(str)
}
return inner
}
var fn = outer()
fn()
解释: 外部函数fn使用了outer函数内部的变量str
3. 内部函数被return
function outer() {
let str = '闭包'
return function inner() {
console.log(str)
}
}
var fn = outer()
fn()
解释: 内部函数inner被return,其实也可以将inner省去,返回一个匿名函数,作用是一样的。
4. 其他写法
function outer() {
let str = '闭包'
return function() {
return str
}
}
var fn = outer()
console.log(fn())
边栏推荐
- unity之粒子特效制作图片拼合文字效果
- The sword refers to Offer 18. Delete the node of the linked list
- 2022-08-07 mysql/stonedb慢SQL-子查询-半连接
- Personal Summary of OLTP and OLAP Issues
- ES6对象字面量的新功能
- Research on Blind Recognition of Digital Modulated Signal Based on MindSpore Framework
- Open3D ICP精配准(使用鲁棒性核函数)
- 国内最主流的5大项目工时管理系统
- The shell updates the terminal output information in place
- QMI8658 - 6轴传感器学习笔记 - Ⅱ
猜你喜欢

Strong Net Cup 2019 - Casual Bet (Stacked Injection)

XDR technology

由联合体union引出的大小端问题

Open3D ICP精配准(使用鲁棒性核函数)

2022-08-07 mysql/stonedb slow SQL-subquery-semi-join

【Review of Live Streaming】Synthesis MindSpore Usability SIG2022 First Half Review Summary

数据在内存如何分布的?

分类、目标检测、语义分割、实例分割的区别

【模板引擎】velocity

拒绝“内卷”跃迁软件测试最大门槛,我是如何从月薪8K到15K的?
随机推荐
机器学习笔记:学习率预热 warmup
走进音视频的世界——RGB与YUV格式
MySQL——索引与事务
MySQL from entry to entry [20W word collection]
leetcode: 455. 分发饼干
Amazon Cloud Technology Build On Learning Experience
C语言-分值和循环语句
基于扰动观察法的光伏mppt最大功率控制matlab仿真
The big and small end problem caused by union union
2022-08-07 mysql/stonedb慢SQL-子查询-半连接
The sword refers to Offer 18. Delete the node of the linked list
This article will give you a thorough understanding of synchronized and Lock
【Win10】若干睡眠问题及对策
Machine Learning Notes: Learning Rate Warmup
[Graph Basics] How to Define Few-Shot Learning on Heterogeneous Graphs: Heterogeneous Graph Few-Shot Learning
NorFlash的存储原理
ToDesk企业版上新 | 十大新功能,让企业远控更安全、更便捷、更流畅
XDR technology
二维码生成工具
CARLA 笔记(05)— Actors and blueprints(创建和修改 Blueprint、生成 Spawning、使用 Handling、销毁 Destruction)