<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title></title> <script type="text/javascript" src="js/jquery-1.12.4.js" ></script> </head> <body> <script type="text/javascript"> var add = (function(){ var count = 0; return function(){ return count += 1; }; })(); var inner = function(count){ return count += 1; }; function test1(){ return inner(0); } var add2 = (function(){ var count = 0; return function(){ count = inner(count); return count; }; })(); // alert(test1()); // alert(inner(0)); var n = 0; for(var i = 0; i < 5; i++){ n = add2(); } alert(n); /*var n = add(); n = 0; for(var i = 0; i < 5; i++){ n = add(); } alert(n);*/ </script> </body> </html>