javascript 闭包真正理解

源码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="jquery.min.js" language="javascript" type="text/javascript"></script>
<script src="wkl_ajax.js" language="javascript" type="text/javascript"></script>
<script type="text/javascript">
function f(x, n) {//求幂
var result = 1;
for(var i = 0; i < n; i++) {
result
*= x;
}
return result;
}

function currying(n) {
return function(x) {
return f(x, n);
};
}

var square = currying(2);
var cube = currying(3);

alert(square(
2));
//4
alert(cube(2));
//8
</script>
</head>
<body>
<div class="me">
test
</div>
</body>
</html>

  

posted @ 2011-08-27 13:37  wangkangluo1  阅读(299)  评论(0编辑  收藏  举报