博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]What is closure
阅读量:7086 次
发布时间:2019-06-28

本文共 3208 字,大约阅读时间需要 10 分钟。

what is closure?

-- A closure is a function that can access interesting non-local variables

 

Variable scope

When you declare a local variable, that variable has a scope. Generally local variables exist only within the block or function in which you declare them.

function() {  var a = 1;  console.log(a); // works}    console.log(a); // fails

If I try to access a local variable, most languages will look for it in the current scope, then up through the parent scopes until they reach the root scope.

var a = 1;function() {  console.log(a); // works}    console.log(a); // works

When a block or function is done with, its local variables are no longer needed and are usually blown out of memory.

This is how we normally expect things to work.

A closure is a persistent local variable scope

A closure is a persistent scope which holds on to local variables even after the code execution has moved out of that block. Languages which support closure (such as JavaScript, Swift and Ruby) will allow you to keep a reference to a scope (including its parent scopes), even after the block in which those variables were declared has finished executing, provided you keep a reference to that block or function somewhere.

The scope object, and all its local variables, are tied to the function, and will persist as long as that function persists.

This gives us function portability. We can expect any variables that were in scope when the function was first defined to still be in scope when we later call the function, even if we call the function in a completely different context.

For example

Here's a really simple example in JavaScript that illustrates the point:

outer = function() {  var a = 1;  var inner = function() {    alert(a);  }  return inner; // this returns a function}var fnc = outer(); // execute outer to get inner fnc();

Here I have defined a function within a function. The inner function gains access to all the outer function's local variables, including a. The variable a is in scope for the inner function.

Normally when a function exits, all its local variables are blown away. However, if we return the inner function and assign it to a variable fnc, so that it persists after outer has exited, all of the variables that were in scope when inner was defined also persist. The variable a has been closed over -- it is within a closure.

Note that the variable a is totally private to fnc. This is a way of creating private variables in a functional programming language such as JavaScript.

As you might be able to guess, when I call fnc() it alerts the value of a, which is "1".

In a language without closure, the variable a would have been garbage collected and thrown away when the function outer exited. Calling fnc would have thrown an error because a no longer exists.

In JavaScript, the variable a persists because variable scope is created when the function is first declared, and persists for as long as the function continues to exist.

a belongs to the scope of outer. The scope of inner has a parent pointer to the scope of outerfnc is a variable which points to innera persists as long as fnc persists. a is within the closure.

转载于:https://www.cnblogs.com/Ice-Max/p/5742100.html

你可能感兴趣的文章
【游戏开发备注之三】GameCenter登陆出现“无法识别此游戏”问题的两种解决方法...
查看>>
轻松获取海量长尾词 网站流量提升不再难
查看>>
sysbench的安装和做性能测试
查看>>
一道简单而又容易出错的题目
查看>>
报告:NVMe存储系统机遇与挑战
查看>>
从趣味游戏到排序算法(4)
查看>>
组策略妙用----通过组策略禁止域用户更改IP地址
查看>>
自助服务台——多渠道触发运维工单
查看>>
暴强贴:从.NET平台调用Win32 API
查看>>
AIX LV删除后,ORACLE数据库文件全部恢复成功
查看>>
BREW中几种常用的效果(淡淡浅出、半透明)
查看>>
jQuery如何得到tagName?
查看>>
一个B/S结构自动二次请求需求的实现
查看>>
.Net Micro Framework研究—串口部署释疑
查看>>
烂泥:学习tomcat之通过shell批量管理多个tomcat
查看>>
mySQL教程 第8章 视图
查看>>
linux dpm机制分析(下)【转】
查看>>
使用ArchiSteamFarm在树莓派挂卡
查看>>
Docker Stack 部署web集群
查看>>
ListBox和ComboBox绑定数据简单例子
查看>>