1 Star 0 Fork 0

math.most/js_study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
019闭包.js 806 Bytes
一键复制 编辑 原始数据 按行查看 历史
Administrator 提交于 2021-05-10 22:59 +08:00 . js up study
/**
* 闭包
*/
function fn1 () {
let a = 1;
let b = 'abc'
function fn2 () { // 执行函数定义一就会产生闭包(不用调用内部函数)
a++;
console.log('start');
console.log(a);
console.log('end');
}
return fn2;
}
let f = fn1();
f(); // 2
f(); // 3
f = null; // 闭包死亡: 包含闭包的函数对象成为垃圾对象
/**
* 闭包基本应用
*/
function myModule () {
let msg = "testMyModule";
function doSomething () {
console.log(msg.toUpperCase());
};
function doOtherthing () {
console.log(msg.toLowerCase());
};
return {
doSomething,
doOtherthing
};
}
let my_module = myModule();
my_module.doSomething(); // TESTMYMODULE
my_module.doOtherthing(); // testmymodule
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/mathmost/js_study.git
git@gitee.com:mathmost/js_study.git
mathmost
js_study
js_study
master

搜索帮助