专栏名称: JavaScript
面向JavaScript爱好人员提供:前端最新资讯、原创内容、JavaScript、HTML5、Ajax、jQuery、Node.js等一系列教程和经验分享。
目录
相关文章推荐
51好读  ›  专栏  ›  JavaScript

用9种办法解决 JS 闭包经典面试题之 for 循环取 i

JavaScript  · 公众号  · Javascript  · 2017-01-06 10:31

正文

转自 https://segmentfault.com/a/1190000003818163


闭包

1.正确的说,应该是指一个闭包域,每当声明了一个函数,它就产生了一个闭包域(可以解释为每个函数都有自己的函数栈),每个闭包域(Function 对象)都有一个 function scope(不是属性),function scope内默认有个名为Global的全局引用(有了这个引用,就可以直接调用 Global 的属性或方法)

2.凡是在闭包域内声明的变量或方法,外部无法直接访问

3.闭包域可以访问外部的变量或方法 
(上图为 chrome 下 debug 环境)

当在一个闭包域内包含另一个闭包域时(简单的说就是在一个函数内有另一个函数,当然这个内部函数的生命周期是依附于外部函数的), 此时,若子闭包域(内部的闭包域,内部函数)使用了父闭包域(外部闭包域,外部函数)的私有变量(在父闭包域中声明的变量,父闭包域的外部空间无法直接访问,但子闭包域可以访问),子闭包域即当前的子函数的 function scope 会产生一个 closure 对象属性,这个对象属性内包含的是子闭包域对父闭包域的所有引用(只要子闭包域(内部函数)还存活,其父闭包域(外部函数)就依旧存活),倘若在父闭包域存活期间对其私有变量内容进行修改,则对这些父闭包域的私有变量进行引用的子闭包域中 function scope 的 closure 对象属性的内容也会发生变化,因为这只是引用.

举例:

lang="en">

    charset="UTF-8">

   </span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="tag" style="color: rgb(0, 0, 136);"/></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="tag" style="color: rgb(0, 0, 136);"/></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="tag" style="color: rgb(0, 0, 136);"><script><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="atn" style="color: teal;">type</span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="atv" style="color: rgb(221, 17, 68);">"text/javascript"</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="atn" style="color: teal;">charset</span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="atv" style="color: rgb(221, 17, 68);">"utf-8"</span><span class="tag" style="color: rgb(0, 0, 136);">></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        </span><span class="com" style="color: rgb(147, 161, 161);">//函数 a 有一个私有变量 p 和一个内部函数 innerA</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        </span><span class="kwd" style="color: rgb(30, 52, 123);">function</span><span class="pln" style="color: rgb(72, 72, 76);"> a</span><span class="pun" style="color: rgb(147, 161, 161);">()</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span><span class="pln" style="color: rgb(72, 72, 76);">                      </span><span class="com" style="color: rgb(147, 161, 161);">//外部闭包域 ,一个名为 a 的 Function 对象</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">            </span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> p </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="lit" style="color: rgb(25, 95, 145);">0</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">                      </span><span class="com" style="color: rgb(147, 161, 161);">//私有变量 p</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">            </span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> innerA </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">function</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">()</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span><span class="pln" style="color: rgb(72, 72, 76);">      </span><span class="com" style="color: rgb(147, 161, 161);">//内部闭包域 ,一个名为 innerA 的 Function 对象</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">                console</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">log</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="pln" style="color: rgb(72, 72, 76);">p</span><span class="pun" style="color: rgb(147, 161, 161);">);</span><span class="pln" style="color: rgb(72, 72, 76);">             </span><span class="com" style="color: rgb(147, 161, 161);">//对外部闭包域的私有变量进行了引用,故 innerA 对象的 function scope 会产生一个名为 closure 的对象属性,closure 对象内含有一个名为 p 的引用</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">            </span><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"> </code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">            innerA</span><span class="pun" style="color: rgb(147, 161, 161);">();</span><span class="com" style="color: rgb(147, 161, 161);">//输出0</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">            p</span><span class="pun" style="color: rgb(147, 161, 161);">++;</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">            innerA</span><span class="pun" style="color: rgb(147, 161, 161);">();</span><span class="com" style="color: rgb(147, 161, 161);">//输出1</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        </span><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        a</span><span class="pun" style="color: rgb(147, 161, 161);">();</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="tag" style="color: rgb(0, 0, 136);"></script></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="tag" style="color: rgb(0, 0, 136);"/></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="tag" style="color: rgb(0, 0, 136);"/></code></p></pre><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">结果如下:</p><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">第一次调用innerA</p><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;"><img data-s="300,640" data-type="png" referrerpolicy="no-referrer" data- referrerpolicy="no-referrer" src="http://mmbiz.qpic.cn/mmbiz_png/btsCOHx9LAPDuD1sYzs7T15G4mDZ9fNFjsf6ZEicXTFeWcJnAAPcHvSqMlSkWJicQhg8WwqVSoFEJ8WKnicuQTMVA/0?wx_fmt=png" data-ratio="1.01067615658363" data-w="281"/><br/></p><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">第二次调用 innerA</p><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;"><img data-s="300,640" data-type="png" referrerpolicy="no-referrer" data- referrerpolicy="no-referrer" src="http://mmbiz.qpic.cn/mmbiz_png/btsCOHx9LAPDuD1sYzs7T15G4mDZ9fNFOBVMia0eUxsXLucia79H16CrXjXx31wf3E6NfSZbibPBO9SeKQcK02XJA/0?wx_fmt=png" data-ratio="0.7066326530612245" data-w="392"/><br/></p><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">控制台输出</p><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;"><img data-s="300,640" data-type="png" referrerpolicy="no-referrer" data- referrerpolicy="no-referrer" src="http://mmbiz.qpic.cn/mmbiz_png/btsCOHx9LAPDuD1sYzs7T15G4mDZ9fNFQMV3tt4PlZjejRMwOjhBhGrE70J7uUIo65USH70p2ZibGfqYOWLQGIA/0?wx_fmt=png" data-ratio="0.1592442645074224" data-w="741"/><br/></p><h4 style=" margin-top: 20px; margin-bottom: 10px; font-weight: bold; font-size: 17px; cursor: text; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; white-space: normal; widows: auto; ">回到主题 面试经典问题</h4><pre class="prettyprint linenums prettyprinted" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid rgb(225, 225, 232); font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; widows: auto; background-color: rgb(247, 247, 249);"><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="dec" style="color: teal;"/></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="tag" style="color: rgb(0, 0, 136);"><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="atn" style="color: teal;">lang</span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="atv" style="color: rgb(221, 17, 68);">"en"</span><span class="tag" style="color: rgb(0, 0, 136);">></span></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="tag" style="color: rgb(0, 0, 136);"/></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="tag" style="color: rgb(0, 0, 136);"><meta/><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="atn" style="color: teal;">charset</span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="atv" style="color: rgb(221, 17, 68);">"UTF-8"</span><span class="tag" style="color: rgb(0, 0, 136);">></span></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="tag" style="color: rgb(0, 0, 136);"><title/></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="tag" style="color: rgb(0, 0, 136);"><script><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="atn" style="color: teal;">type</span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="atv" style="color: rgb(221, 17, 68);">"text/javascript"</span><span class="tag" style="color: rgb(0, 0, 136);">></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        </span><span class="com" style="color: rgb(147, 161, 161);">//面试经典问题:</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"> </code> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        </span><span class="kwd" style="color: rgb(30, 52, 123);">function</span><span class="pln" style="color: rgb(72, 72, 76);"> onMyLoad</span><span class="pun" style="color: rgb(147, 161, 161);">(){</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">            </span><span class="com" style="color: rgb(147, 161, 161);">/*</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">            抛出问题:</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">                此题的目的是想每次点击对应目标时弹出对应的数字下标 0~4,但实际是无论点击哪个目标都会弹出数字5</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">            问题所在:</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">                arr 中的每一项的 onclick 均为一个函数实例(Function 对象),这个函数实例也产生了一个闭包域,</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">                这个闭包域引用了外部闭包域的变量,其 function scope 的 closure 对象有个名为 i 的引用,</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">                外部闭包域的私有变量内容发生变化,内部闭包域得到的值自然会发生改变</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">            */</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">            </span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> arr </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> document</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">getElementsByTagName</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="str" style="color: rgb(221, 17, 68);">"p"</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">            </span><span class="kwd" style="color: rgb(30, 52, 123);">for</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> i </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="lit" style="color: rgb(25, 95, 145);">0</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);"> i </span><span class="pun" style="color: rgb(147, 161, 161);"><</span><span class="pln" style="color: rgb(72, 72, 76);"> arr</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">length</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">++){</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">                arr</span><span class="pun" style="color: rgb(147, 161, 161);">[</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">].</span><span class="pln" style="color: rgb(72, 72, 76);">onclick </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">function</span><span class="pun" style="color: rgb(147, 161, 161);">(){</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">                    alert</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">                </span><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">            </span><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        </span><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="tag" style="color: rgb(0, 0, 136);"></script></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="tag" style="color: rgb(0, 0, 136);"/></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="tag" style="color: rgb(0, 0, 136);"><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="atn" style="color: teal;">onload</span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="atv" style="color: rgb(221, 17, 68);">"</span><span class="pln" style="color: rgb(72, 72, 76);">onMyLoad</span><span class="pun" style="color: rgb(147, 161, 161);">()</span><span class="atv" style="color: rgb(221, 17, 68);">"</span><span class="tag" style="color: rgb(0, 0, 136);">></span></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="tag" style="color: rgb(0, 0, 136);"><p/></span><span class="pln" style="color: rgb(72, 72, 76);">产品一</span><span class="tag" style="color: rgb(0, 0, 136);"/></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="tag" style="color: rgb(0, 0, 136);"><p/></span><span class="pln" style="color: rgb(72, 72, 76);">产品二</span><span class="tag" style="color: rgb(0, 0, 136);"/></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="tag" style="color: rgb(0, 0, 136);"><p/></span><span class="pln" style="color: rgb(72, 72, 76);">产品三</span><span class="tag" style="color: rgb(0, 0, 136);"/></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="tag" style="color: rgb(0, 0, 136);"><p/></span><span class="pln" style="color: rgb(72, 72, 76);">产品四</span><span class="tag" style="color: rgb(0, 0, 136);"/></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="tag" style="color: rgb(0, 0, 136);"><p/></span><span class="pln" style="color: rgb(72, 72, 76);">产品五</span><span class="tag" style="color: rgb(0, 0, 136);"/></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="tag" style="color: rgb(0, 0, 136);"/></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="tag" style="color: rgb(0, 0, 136);"/></code></p></pre><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">解决办法:</p><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">解决办法一</p><pre class="prettyprint linenums prettyprinted" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid rgb(225, 225, 232); font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; widows: auto; background-color: rgb(247, 247, 249);"><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">/*</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">解决思路:</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">    增加若干个对应的闭包域空间(这里采用的是匿名函数),专门用来存储原先需要引用的内容(下标),不过只限于基本类型(基本类型值传递,对象类型引用传递)</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);"> */</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="kwd" style="color: rgb(30, 52, 123);">for</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> i </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="lit" style="color: rgb(25, 95, 145);">0</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);"><span class="pln" style="color: rgb(72, 72, 76);">arr</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">length</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">++){</span></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"> </code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="com" style="color: rgb(147, 161, 161);">//声明一个匿名函数,若传进来的是基本类型则为值传递,故不会对实参产生影响,</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="com" style="color: rgb(147, 161, 161);">//该函数对象有一个本地私有变量arg(形参) ,该函数的 function scope 的 closure 对象属性有两个引用,一个是 arr,一个是 i</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="com" style="color: rgb(147, 161, 161);">//尽管引用 i 的值随外部改变 ,但本地私有变量(形参) arg 不会受影响,其值在一开始被调用的时候就决定了.</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="kwd" style="color: rgb(30, 52, 123);">function</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="pln" style="color: rgb(72, 72, 76);">arg</span><span class="pun" style="color: rgb(147, 161, 161);">)</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        arr</span><span class="pun" style="color: rgb(147, 161, 161);">[</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">].</span><span class="pln" style="color: rgb(72, 72, 76);">onclick </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">function</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">()</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span><span class="pln" style="color: rgb(72, 72, 76);">  </span><span class="com" style="color: rgb(147, 161, 161);">//onclick函数实例的 function scope 的 closure 对象属性有一个引用 arg,</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">            alert</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="pln" style="color: rgb(72, 72, 76);">arg</span><span class="pun" style="color: rgb(147, 161, 161);">);</span><span class="pln" style="color: rgb(72, 72, 76);">                 </span><span class="com" style="color: rgb(147, 161, 161);">//只要 外部空间的 arg 不变,这里的引用值当然不会改变</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        </span><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="pun" style="color: rgb(147, 161, 161);">})(</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">);</span><span class="pln" style="color: rgb(72, 72, 76);">                              </span><span class="com" style="color: rgb(147, 161, 161);">//立刻执行该匿名函数,传递下标 i(实参)</span></code> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p></pre><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">解决办法二</p><pre class="prettyprint linenums prettyprinted" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid rgb(225, 225, 232); font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; widows: auto; background-color: rgb(247, 247, 249);"><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">/*</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">解决思路:</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">    将下标作为对象属性(name:"i",value:i的值)添加到每个数组项(p对象)中</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">*/</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="kwd" style="color: rgb(30, 52, 123);">for</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> i </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="lit" style="color: rgb(25, 95, 145);">0</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);"><span class="pln" style="color: rgb(72, 72, 76);">arr</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">length</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">++){</span></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="com" style="color: rgb(147, 161, 161);">//为当前数组项即当前 p 对象添加一个名为 i 的属性,值为循环体的 i 变量的值,</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="com" style="color: rgb(147, 161, 161);">//此时当前 p 对象的 i 属性并不是对循环体的 i 变量的引用,而是一个独立p 对象的属性,属性值在声明的时候就确定了</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="com" style="color: rgb(147, 161, 161);">//(基本类型的值都是存在栈中的,当有一个基本类型变量声明其等于另一个基本变量时,此时并不是两个基本类型变量都指向一个值,而是各自有各自的值,但值是相等的)</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    arr</span><span class="pun" style="color: rgb(147, 161, 161);">[</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">].</span><span class="pln" style="color: rgb(72, 72, 76);">i </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> i</span><span class="pun" style="color: rgb(147, 161, 161);">;</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    arr</span><span class="pun" style="color: rgb(147, 161, 161);">[</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">].</span><span class="pln" style="color: rgb(72, 72, 76);">onclick </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">function</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">()</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        alert</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="kwd" style="color: rgb(30, 52, 123);">this</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p></pre><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">解决办法三</p><pre class="prettyprint linenums prettyprinted" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid rgb(225, 225, 232); font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; widows: auto; background-color: rgb(247, 247, 249);"><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">/*</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">解决思路:</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">    与解决办法一有点相似但却有点不太相似.</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">    相似点:同样是增加若干个对应的闭包域空间用来存储下标</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">    不同点:解决办法一是在新增的匿名闭包空间内完成事件的绑定,而此例是将事件绑定在新增的匿名函数返回的函数上</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"> </code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">    此时绑定的函数中的 function scope 中的 closure 对象的 引用 arg 是指向将其返回的匿名函数的私有变量 arg</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);"> */</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="kwd" style="color: rgb(30, 52, 123);">for</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> i </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="lit" style="color: rgb(25, 95, 145);">0</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);"> i</span><span class="pun" style="color: rgb(147, 161, 161);"><span class="pln" style="color: rgb(72, 72, 76);">arr</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">length</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">++){</span></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    arr</span><span class="pun" style="color: rgb(147, 161, 161);">[</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">].</span><span class="pln" style="color: rgb(72, 72, 76);">onclick </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="kwd" style="color: rgb(30, 52, 123);">function</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="pln" style="color: rgb(72, 72, 76);">arg</span><span class="pun" style="color: rgb(147, 161, 161);">){</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        </span><span class="kwd" style="color: rgb(30, 52, 123);">return</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">function</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">()</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">            alert</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="pln" style="color: rgb(72, 72, 76);">arg</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        </span><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="pun" style="color: rgb(147, 161, 161);">})(</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p></pre><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">解决办法四</p><pre class="prettyprint linenums prettyprinted" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid rgb(225, 225, 232); font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; widows: auto; background-color: rgb(247, 247, 249);"><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">/*</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">解决思路与解决办法一相同</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);"> */</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="kwd" style="color: rgb(30, 52, 123);">for</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> i </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="lit" style="color: rgb(25, 95, 145);">0</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);"> i</span><span class="pun" style="color: rgb(147, 161, 161);"><span class="pln" style="color: rgb(72, 72, 76);">arr</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">length</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">++){</span></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="kwd" style="color: rgb(30, 52, 123);">function</span><span class="pun" style="color: rgb(147, 161, 161);">(){</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">       </span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> temp </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> i</span><span class="pun" style="color: rgb(147, 161, 161);">;</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        arr</span><span class="pun" style="color: rgb(147, 161, 161);">[</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">].</span><span class="pln" style="color: rgb(72, 72, 76);">onclick </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">function</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">()</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span></code> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">            alert</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="pln" style="color: rgb(72, 72, 76);">temp</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        </span><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="pun" style="color: rgb(147, 161, 161);">})();</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p></pre><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">解决办法五</p><pre class="prettyprint linenums prettyprinted" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid rgb(225, 225, 232); font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; widows: auto; background-color: rgb(247, 247, 249);"><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">/*</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">解决思路与解决办法三及四相同</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);"> */</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="kwd" style="color: rgb(30, 52, 123);">for</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> i </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="lit" style="color: rgb(25, 95, 145);">0</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);"><span class="pln" style="color: rgb(72, 72, 76);">arr</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">length</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">++){</span></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    arr</span><span class="pun" style="color: rgb(147, 161, 161);">[</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">].</span><span class="pln" style="color: rgb(72, 72, 76);">onclick </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="kwd" style="color: rgb(30, 52, 123);">function</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">()</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        </span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> temp </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> i</span><span class="pun" style="color: rgb(147, 161, 161);">;</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        </span><span class="kwd" style="color: rgb(30, 52, 123);">return</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">function</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">()</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">            alert</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="pln" style="color: rgb(72, 72, 76);">temp</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        </span><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="pun" style="color: rgb(147, 161, 161);">})();</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p></pre><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">解决办法六</p><pre class="prettyprint linenums prettyprinted" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid rgb(225, 225, 232); font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; widows: auto; background-color: rgb(247, 247, 249);"><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">/*</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">解决思路:</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">    将下标添加为绑定函数的属性</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);"> */</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="kwd" style="color: rgb(30, 52, 123);">for</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> i </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="lit" style="color: rgb(25, 95, 145);">0</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);"><span class="pln" style="color: rgb(72, 72, 76);">arr</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">length</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">++){</span></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="pln" style="color: rgb(72, 72, 76);">arr</span><span class="pun" style="color: rgb(147, 161, 161);">[</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">].</span><span class="pln" style="color: rgb(72, 72, 76);">onclick </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">function</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">()</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        alert</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="pln" style="color: rgb(72, 72, 76);">arguments</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">callee</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">);</span><span class="pln" style="color: rgb(72, 72, 76);">      </span><span class="com" style="color: rgb(147, 161, 161);">//arguments 参数对象  arguments.callee 参数对象所属函数</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="pun" style="color: rgb(147, 161, 161);">}).</span><span class="pln" style="color: rgb(72, 72, 76);">i </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> i</span><span class="pun" style="color: rgb(147, 161, 161);">;</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p></pre><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">解决办法七</p><pre class="prettyprint linenums prettyprinted" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid rgb(225, 225, 232); font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; widows: auto; background-color: rgb(247, 247, 249);"><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">/*</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">解决思路:</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">    通过 new 使用 Function 的构造函数 创建 Function 实例实现,由于传入的函数体的内容是字符串,故 Function 得到的是一个字符串拷贝,而没有得到 i 的引用(这里是先获取 i.toString()然后与前后字符串拼接成一个新的字符串,Function 对其进行反向解析成 JS 代码)</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);"> */</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="kwd" style="color: rgb(30, 52, 123);">for</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> i </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="lit" style="color: rgb(25, 95, 145);">0</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);"><span class="pln" style="color: rgb(72, 72, 76);">arr</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">length</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">++){</span></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    arr</span><span class="pun" style="color: rgb(147, 161, 161);">[</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">].</span><span class="pln" style="color: rgb(72, 72, 76);">onclick </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">new</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">Function</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="str" style="color: rgb(221, 17, 68);">"alert("</span><span class="pun" style="color: rgb(147, 161, 161);">+</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">+</span><span class="str" style="color: rgb(221, 17, 68);">");"</span><span class="pun" style="color: rgb(147, 161, 161);">);</span><span class="com" style="color: rgb(147, 161, 161);">//每 new 一个 Function 得到一个 Function 对象(一个函数),有自己的闭包域</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p></pre><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">解决办法八</p><pre class="prettyprint linenums prettyprinted" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid rgb(225, 225, 232); font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; widows: auto; background-color: rgb(247, 247, 249);"><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">/*</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">解决思路:</span></code> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">    直接通过 Function 返回一个函数</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">    与解决办法七的不同之处在于:</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">        解决办法七使用 new,使用了 new,此时 Function 函数就被当成构造器可以用来构造一个 Function 实例返回</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">        当前解决办法没有使用 new ,即将 Function 函数当成一个函数,传入参数返回一个新函数;</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">        其实此处 new 与不 new 只是的区别在于:</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">            使用了 new 即 Function 函数充当构造器,由 JS 解析器生产一个新的对象,构造器内的 this 指向该新对象;</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">            不实用 new 即 Function 函数依旧是函数,由函数内部自己生产一个实例返回.</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);"> */</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="kwd" style="color: rgb(30, 52, 123);">for</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> i </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="lit" style="color: rgb(25, 95, 145);">0</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);"><span class="pln" style="color: rgb(72, 72, 76);">arr</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">length</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">++){</span></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    arr</span><span class="pun" style="color: rgb(147, 161, 161);">[</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">].</span><span class="pln" style="color: rgb(72, 72, 76);">onclick </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">Function</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="str" style="color: rgb(221, 17, 68);">"alert("</span><span class="pun" style="color: rgb(147, 161, 161);">+</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">+</span><span class="str" style="color: rgb(221, 17, 68);">");"</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p></pre><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">解决办法九 <br/>使用ES6新语法 let 关键字 由于几新东西 各浏览器支持不同 <br/>chrome 及 opera支持以下语法</p><pre class="prettyprint linenums prettyprinted" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid rgb(225, 225, 232); font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; widows: auto; background-color: rgb(247, 247, 249);"><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="tag" style="color: rgb(0, 0, 136);"><script><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="atn" style="color: teal;">type</span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="atv" style="color: rgb(221, 17, 68);">"application/javascript"</span><span class="tag" style="color: rgb(0, 0, 136);">></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="str" style="color: rgb(221, 17, 68);">"use strict"</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="com" style="color: rgb(147, 161, 161);">//使用严格模式,否则报错 SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> arr </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> document</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">getElementsByTagName</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="str" style="color: rgb(221, 17, 68);">"p"</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="kwd" style="color: rgb(30, 52, 123);">for</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> i </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="lit" style="color: rgb(25, 95, 145);">0</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);"><</span><span class="pln" style="color: rgb(72, 72, 76);">arr</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">length</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">++){</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        let j </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> i</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="com" style="color: rgb(147, 161, 161);">//创建一个块级变量</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        arr</span><span class="pun" style="color: rgb(147, 161, 161);">[</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">].</span><span class="pln" style="color: rgb(72, 72, 76);">onclick </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">function</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">()</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">            alert</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="pln" style="color: rgb(72, 72, 76);">j</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        </span><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="tag" style="color: rgb(0, 0, 136);"></script></span></code></p></pre><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">在 chrome 查看</p><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;"><img data-s="300,640" data-type="png" referrerpolicy="no-referrer" data- referrerpolicy="no-referrer" src="http://mmbiz.qpic.cn/mmbiz_png/btsCOHx9LAPDuD1sYzs7T15G4mDZ9fNFia6D72sdwsws1tuSd5BtFTvsiaMsOBxJOx7cjkuzy6wgyZRfoicMnAVTA/0?wx_fmt=png" data-ratio="0.48625" data-w="800"/><br/></p><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">可以在控制台看到 j 变量是一个 block 级的变量</p><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">待函数绑定完成后看数组项:</p><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;"><img data-s="300,640" data-type="png" referrerpolicy="no-referrer" data- referrerpolicy="no-referrer" src="http://mmbiz.qpic.cn/mmbiz_png/btsCOHx9LAPDuD1sYzs7T15G4mDZ9fNFIKYNE3vFQ48PspAkJkhlWgKNLneXWMRIGNYBfxwkKlgeRlrxXJaX8w/0?wx_fmt=png" data-ratio="0.486646884272997" data-w="337"/><br/></p><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">此时的该数组项的<span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, 'PingFang SC', 'Hiragino Sans GB', 'WenQuanYi Micro Hei', 'Microsoft Yahei', sans-serif; font-size: 14px; line-height: 22.4px; background-color: rgb(255, 255, 255);"><function scope=""/></span><function scope="">的 Block 域有个 j 存储的就是对应的数组下标 <br/>firefox支持一下语法</function></p><pre class="prettyprint linenums prettyprinted" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid rgb(225, 225, 232); font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; widows: auto; background-color: rgb(247, 247, 249);"><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="tag" style="color: rgb(0, 0, 136);"><script><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="atn" style="color: teal;">type</span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="atv" style="color: rgb(221, 17, 68);">"application/javascript;version=1.7"</span><span class="tag" style="color: rgb(0, 0, 136);">></span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> arr </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> document</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">getElementsByTagName</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="str" style="color: rgb(221, 17, 68);">"p"</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="kwd" style="color: rgb(30, 52, 123);">for</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="kwd" style="color: rgb(30, 52, 123);">var</span><span class="pln" style="color: rgb(72, 72, 76);"> i </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="lit" style="color: rgb(25, 95, 145);">0</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);"><</span><span class="pln" style="color: rgb(72, 72, 76);">arr</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">length</span><span class="pun" style="color: rgb(147, 161, 161);">;</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">++){</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        let j </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> i</span><span class="pun" style="color: rgb(147, 161, 161);">;</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        arr</span><span class="pun" style="color: rgb(147, 161, 161);">[</span><span class="pln" style="color: rgb(72, 72, 76);">i</span><span class="pun" style="color: rgb(147, 161, 161);">].</span><span class="pln" style="color: rgb(72, 72, 76);">onclick </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">function</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">()</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">            alert</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="pln" style="color: rgb(72, 72, 76);">j</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">        </span><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="pln" style="color: rgb(72, 72, 76);">    </span><span class="pun" style="color: rgb(147, 161, 161);">}</span></code></p><p><code style="border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; max-width: 100%; background-color: transparent;"><span class="tag" style="color: rgb(0, 0, 136);"></script></span></code></p></pre><p style="margin-top: 10px; margin-bottom: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">由于新语法各大厂商的支持尚未规范故暂不不推荐使用</p><p style="margin-top: 10px; font-size: 15px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; line-height: 24px; white-space: normal; widows: auto;">解决办法大同小异,只要理解其中的实质,可以写出多多的解决办法</p><p><br/></p><p>点击阅读全文,查看更多<img data-s="300,640" data-type="jpeg" referrerpolicy="no-referrer" data- referrerpolicy="no-referrer" src="http://mmbiz.qpic.cn/mmbiz/btsCOHx9LAO8DgbKOp1N3kXCm2abOicwu2ibJYYhrwfnuOQ05yAYkU5ia0ibBSCLTA9NuPc53VhpP4C3DWDt5OIOicA/0?wx_fmt=jpeg" data-ratio="0.8967297762478486" data-w="581"/><br/></p> </div> </div> </div> </div> <div class="topic_buttons"> <div class="fr gray f11" style="line-height: 12px; padding-top: 3px; text-shadow: 0px 1px 0px #fff;"> </div> </div> </div> <div class="sep5"></div> <div class="box"> <div class="cell"> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- jintian_wenzhi --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-1837452791782084" data-ad-slot="8042312523" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="sep5"></div> <div class="sep10"></div> <div class="box"> <div class="cell"> <span class="gray">推荐文章</span> </div> <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="32" valign="top" align="center"> <img src="http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/mmbiz_jpg/r01vJcW81fHY6sY4vAA2VNdzv16BibZ9dNfibSANCMOPCmdCAaTSV2icFUuibPz9E7Gyicar45TaAFickrMwpVW5KLdw/0?wx_fmt=jpeg" referrerpolicy="no-referrer" class="avatar" style="max-width: 32px; max-height: 32px;" border="0" align="default"> </td> <td width="10" valign="top"></td> <td width="auto" valign="top" align="left"> <span class="item_hot_topic_title"> <span>奥斯CAR</span>  ·  <a href="/t/cHhza0l5!2VkbA==">车民商城双12进入倒计时,多款爆品减减减!</a> </span> <div class="sep5"></div> <span class="fade">7 年前</span> </td> </tr> </tbody> </table> </div> <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="32" valign="top" align="center"> <img src="http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/mmbiz_png/G8vkERUJibkvkJSatGadq0dN1EicfhJP9sYfHXgD43cjbaX1X7Mpib6qN0Or9KVZm4G9MGRNkbyZ5dh4tQksLS4FA/0?wx_fmt=png" referrerpolicy="no-referrer" class="avatar" style="max-width: 32px; max-height: 32px;" border="0" align="default"> </td> <td width="10" valign="top"></td> <td width="auto" valign="top" align="left"> <span class="item_hot_topic_title"> <span>钱江晚报</span>  ·  <a href="/t/!G1XQnEwa3Fp!A==">中国人穿纸尿裤高考?!男星上韩国节目猛黑中国,火力全开啊!</a> </span> <div class="sep5"></div> <span class="fade">7 年前</span> </td> </tr> </tbody> </table> </div> <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="32" valign="top" align="center"> <img src="http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/mmbiz_jpg/EJfBYyWDbE8FPiaTbvC8QTLvrVq0ib2uFxhBTAYHmDcNOg60HwoRO2zpiayIVg2PomiabQTYDDeicibAeECz2nd6oTWw/0?wx_fmt=jpeg" referrerpolicy="no-referrer" class="avatar" style="max-width: 32px; max-height: 32px;" border="0" align="default"> </td> <td width="10" valign="top"></td> <td width="auto" valign="top" align="left"> <span class="item_hot_topic_title"> <span>上海新闻广播</span>  ·  <a href="/t/SDQ0eU92!WJFcw==">声音日历0119 |最后一卷柯达胶卷拍出的照片</a> </span> <div class="sep5"></div> <span class="fade">7 年前</span> </td> </tr> </tbody> </table> </div> <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="32" valign="top" align="center"> <img src="http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/mmbiz_jpg/wfKwOUXqfJB3WoufAjdRWcswg2ndyfshOCbf5kT9AxAKFhCf6z7Zxsrfs0P5gMzRBSHceiaeNBwHgktWVicWXe3A/0?wx_fmt=jpeg" referrerpolicy="no-referrer" class="avatar" style="max-width: 32px; max-height: 32px;" border="0" align="default"> </td> <td width="10" valign="top"></td> <td width="auto" valign="top" align="left"> <span class="item_hot_topic_title"> <span>BIE别的</span>  ·  <a href="/t/SzlqcW9vTWtRVw==">北京的哪些地方被唱进了歌里?</a> </span> <div class="sep5"></div> <span class="fade">7 年前</span> </td> </tr> </tbody> </table> </div> <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="32" valign="top" align="center"> <img src="http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/mmbiz_jpg/dCSBZibaHmgezia3nISquHPu0mRoOXxw2EvAoVPwbJ7t9xlYQmyyXwVaaKgvicJbkXndypl6HkzVOVsSQAcXtPd2A/0?wx_fmt=jpeg" referrerpolicy="no-referrer" class="avatar" style="max-width: 32px; max-height: 32px;" border="0" align="default"> </td> <td width="10" valign="top"></td> <td width="auto" valign="top" align="left"> <span class="item_hot_topic_title"> <span>佛山发布</span>  ·  <a href="/t/cmxoNGtUNW9jWg==">别买!这些进口食品都被禁了!@佛山街坊</a> </span> <div class="sep5"></div> <span class="fade">7 年前</span> </td> </tr> </tbody> </table> </div> </div> <div class="sep5"></div> <div class="box"> <div class="cell"> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- jintian_wenzhi --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-1837452791782084" data-ad-slot="8042312523" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="sep5"></div> </div> </div> <div class="c"></div> <div class="sep20"></div> </div> <div id="Bottom"> <div class="content footer"> <div class="inner"> <div class="sep10"></div> <div class="fr"> </div> <strong> <a href="http://www.sov5.org" class="dark" target="_blank">Sov5搜索</a>   <span class="snow">·</span>   <a href="http://baike.sov5.org" class="dark" target="_blank">小百科</a>   <span class="snow">·</span>   <a class="go-mobile" href="javascript:;">移动版</a> </strong> <div class="sep20"></div> 51好读 - 好文章就要读起来! </div> </div> </div> <script type="text/javascript" src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/highlight.js/9.2.0/highlight.min.js"></script> <script type="text/javascript" src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript" src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/jqueryui/1.9.2/jquery-ui.min.js"></script> <script type="text/javascript" src="/static/js/vendors/jquery/jquery.autosize.js?v=ac4d62e3842f55aa2b78d2c2ef1af833"></script> <script type="text/javascript" src="/static/js/vendors/lscache/lscache.min.js?v=bf403ab76d287d394375662defac76c3"></script> <script type="text/javascript" src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/select2/3.0.0/select2.min.js"></script> <script type="text/javascript" src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery.selectboxit/3.8.0/jquery.selectBoxIt.min.js"></script> <script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery.textcomplete/0.3.3/jquery.textcomplete.min.js" type="text/javascript"></script> <script type="text/javascript" src="/static/js/site/base/common.js?v=97646d06e4a04b2bf43b7b467cfd321e"></script> <script type="text/javascript" src="/static/js/site/base/v2ex.js?v=d41d8cd98f00b204e9800998ecf8427e"></script> <script type="text/javascript" src="/static/js/site/base/base.js?v=edcfd5298fe2acdc62d7fb498c373f99"></script> <!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-0SBZ564CXS"></script> <script> window.dataLayer = window.dataLayer || []; function gtag() { dataLayer.push(arguments); } gtag('js', new Date()); gtag('config', 'G-0SBZ564CXS'); </script> <script> var _hmt = _hmt || []; (function () { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?55a854de13a9633b2937a3c24817fc7b"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <script> (function () { var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> <!-- 多条广告如下脚本只需引入一次 --> <script type="text/javascript" src="//cpro.baidustatic.com/cpro/ui/cm.js" async="async" defer="defer"> </script> <script type="text/javascript" src="/static/js/site/post/post.js?v=e7935b685b5f2aef4de680492fd620cf"></script> <script type="text/javascript"> jQuery.browser = {}; (function () { jQuery.browser.msie = false; jQuery.browser.version = 0; if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) { jQuery.browser.msie = true; jQuery.browser.version = RegExp.$1; } })(); </script> <script type="text/javascript" src="/static/js/site/post/post_common.js?v=15968870dc6780d5664012d59b217894"></script> <script type="text/javascript" src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery-hashchange/1.3/jquery.ba-hashchange.min.js"></script> <script type="text/javascript" src="/static/js/site/post/post_folder.js?v=d3abab84fa5b19533d2131bf98e690f7"></script> </body> </html>