varadd=function(x){var sum =1;vartmp=function(x){
sum = sum + x;return tmp;}
tmp.toString=function(){return sum;}return tmp;}alert(add(1)(2)(3));//6,第一次调用sum为1,无变化不管里面形参实参多少
案例10
var lis = document.getElementsByTagName("li");for(var i=0;i<lis.length;i++){(function(i){
lis[i].onclick=function(){
console.log(i);};})(i);//事件处理函数中闭包的写法}
案例11
functionm1(){var x =1;returnfunction(){
console.log(++x);}}m1()();//2m1()();//2m1()();//2var m2 =m1();m2();//2m2();//3m2();//4
案例12
var fn=(function(){var i=10;functionfn(){
console.log(++i);}return fn;})()fn();//11fn();//12
案例13
functionlove1(){var num =223;varme1=function(){
console.log(num);}
num++;return me1;}var loveme1 =love1();loveme1();//输出224
案例14
functionfun(n,o){
console.log(o);return{
fun:function(m){returnfun(m,n);}};}var a =fun(0);//undefined
a.fun(1);//0
a.fun(2);//0
a.fun(3);//0 var b =fun(0).fun(1).fun(2).fun(3);//undefined 0 1 2var c =fun(0).fun(1);
c.fun(2);
c.fun(3);//undefined 0 1 1
案例15
functionfn(){var arr =[];for(var i =0;i <5;i ++){
arr[i]=function(){return i;}}return arr;}var list =fn();for(var i =0,len = list.length;i < len ; i ++){
console.log(list[i]());}//5 5 5 5 5
案例16
functionfn(){var arr =[];for(var i =0;i <5;i ++){
arr[i]=(function(i){returnfunction(){return i;};})(i);}return arr;}var list =fn();for(var i =0,len = list.length;i < len ; i ++){
console.log(list[i]());}//0 1 2 3 4