<!DOCTYPE html>
<html >
<head lang="en">
<meta charset="UTF-8">
<title>T68-闭包的理解</title>
</head>
<body>
<p>通过闭包可以从函数外部获取函数内部的变量,如下例子,要在函数f1外部获得n的值,可以在f1函数内部弄一个函数f2,
再把 f2 return出去。然后调用函数f1即可拿到变量 n 。</p>
</body>
<script type="text/javascript">
function f1(){
var n=999;
nAdd=function(){n+=1};
function f2(){
alert(n);
}
return f2;
}
var result=f1();
result(); // 999
nAdd();
result(); // 1000
</script>
</html>