js中的闭包实现自增

来源:互联网 发布:core js 编辑:程序博客网 时间:2024/06/04 17:59

参考自 http://lovejavaei.iteye.com/blog/401167

js脚本之家

http://www.jb51.net/article/54639.htm

http://www.jb51.net/article/24101.htm

http://www.jb51.net/article/24156.htm

js闭包实现 实现选项卡的效果(闭包) : http://www.mekkey.com/index.php/2013/06/13/js_tabs/

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试</title>
<script type="text/javascript"> 
function f(x) { 
var a = 0; 
a++; 
x++; 
var inner = function() { 
return a + x; 

return inner; 

// var test = f(1); 
</script> 
<script type="text/javascript">
function testTime(){
var date1 = new Date();
var m;
for(var i=0;i<10000;i++){
m = i;
}
var date2 = new Date();
for(var i=9999;i>=0;i--){
m = i;
}
var date3 = new Date();
console.log(date2-date1);
console.log(date3-date2);
test1.value=date2-date1;
test2.value=date3-date2;
}
var v=0;
function test(){
incrementText.value=v++;
}
//js闭包实现自增
function incrementClosure(){
// var i = incrementText.value;
var i = 0;
function increment(){
i++;
   return i;
}
return increment;
}
var textValue = incrementClosure();
function testClosure(obj){
// var textValue = incrementClosure();
incrementText.value = textValue();
}
</script>
</head>
<body>
<input type="button" value="测试for循环时间" onclick="testTime()"/>
<input type="text" id="test1"/>
<input type="text" id="test2"/><br/><br/>
<input type="button" value="测试" onclick="test()"/><br/><br/>
<input type="button" value="测试js闭包" onclick="testClosure(this)"/>
<input type ="text" id="incrementText"/>
</body>
</html>


0 0