``````  function f1(){
var n=999;
n+=1
};
function f2(){
}
return f2;
}
var result1=f1();
var result2=f1();
result1(); // 999
result2();//999
result1(); // 是999而不是1000，这是为何呢？
result2();//1000``````

``````  var nAdd = undefined;
function f1(){
var n=999;
n+=1
};
function f2(){
}
return f2;
}
var result1=f1();
var result2=f1();
result1(); // 999
result2();//999
result1(); // 是999而不是1000，这是为何呢？
result2();//1000``````

``````var result1=f1();
var result2=f1();``````

`````` function f1(){
var n=999;
//n在result1中的引用为temp1
var temp1 = n;
temp1 += 1;
};
function f2(){
}
return f2;
}``````

``````function f1(){
var n=999;
//n在result2中的引用为temp2
var temp2 = n;
temp2 += 1;
};
function f2(){
}
return f2;
}``````

``````    var result1=f1();
var result2=f1();
result1(); // 999
result2();//999
result1(); // 999
result2();//1000
result1(); // 999
result2();//1001``````

### 声明提升(Hoisting)

``````var x= 0;
var f=function(){
x=1;
};
f();
function f(){
x = 2;
}
f();

``````var x;
var y;
function f(){
x = 2;
}
x = 0;
f = function(){
x = 1;
}
f();
f();

``````(function() {
var foo = 1;
alert(foo +   + bar +   + baz);
var bar = 2;
var baz = 3;
})();``````

``````(function() {
var foo;
var bar;
var baz;

foo = 1;
alert(foo +   + bar +   + baz);
bar = 2;
baz = 3;
})();``````

``````foo();
function foo() {
}
//其等效的代码
function foo() {
}
foo();``````

``````foo();    //undefined is not a function
var foo = function() {
};``````

### 闭包

1、将内部函数作为值从外部函数返回

``````function B()
{
var temp=abc;
function A()
{