javascript 闭包理解
javascript 闭包理解
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/2000/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>*</title> 6 <script src="jquery.min.js" language="javascript" type="text/javascript"></script> 7 <script src="wkl_ajax.js" language="javascript" type="text/javascript"></script> 8 <script type="text/javascript"> 9 function f(x, n) {//求幂 10 var result = 1; 11 for(var i = 0; i < n; i++) { 12 result *= x; 13 } 14 return result; 15 } 16 17 function currying(n) { 18 return function(x) { 19 return f(x, n); 20 }; 21 } 22 23 var square = currying(2); 24 var cube = currying(3); 25 26 alert(square(2)); 27 //4 28 alert(cube(2)); 29 //8 30 </script> 31 </head> 32 <body> 33 <div class="me"> 34 test 35 </div> 36 </body> 37 </html>