本帖最后由 罗玉宁 于 2013-3-21 17:57 编辑
为什么输出的全是5 而不是12345?- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title>匿名函数</title>
- <meta name="Generator" content="EditPlus">
- <meta name="Author" content="">
- <meta name="Keywords" content="">
- <meta name="Description" content="">
- </head>
- <script type="text/javascript" >
- function box() {
- var arr = [];
-
- for (var i = 0; i < 5; i ++) {
- arr[i] = function () { //arr[0] = 0,arr[1] = 1 .... arr[4] = 4
- return i;
- };
- }
- return arr;
- }
- var b = box();
- for (var i = 0; i < 5; i ++) {
- alert(b[i]());
- }
- </script>
- <body>
-
- </body>
- </html>
复制代码 |