亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于JS的內存泄漏跟JS代碼的規范問題大家是怎么處理的?

關于JS的內存泄漏跟JS代碼的規范問題大家是怎么處理的?

HUX布斯 2018-12-07 12:19:31
關于JS的內存泄漏跟JS代碼的規范問題大家是怎么處理的? 比如這篇文章(JavaScript 中的內存泄露模式)中提到為防止內存泄露,把代碼寫成這樣: 方法一: <html> <body> <script type="text/javascript"> document.write("Avoiding a memory leak by adding another closure"); window.onload=function outerFunction(){ var anotherObj =function innerFunction() { // Some logic here alert("Hi! I have avoided the leak"); }; (function anotherInnerFunction(){ var obj = document.getElementById("element"); obj.onclick=anotherObj })(); }; </script> <button id="element">"Click Here"</button> </body> </html> 方法二: <html> <head> <script type="text/javascript"> document.write("Avoid leaks by avoiding closures!"); window.onload=function() { var obj = document.getElementById("element"); obj.onclick = doesNotLeak; } function doesNotLeak() { //Your Logic here alert("Hi! I have avoided the leak"); } </script> </head> <body> <button id="element">"Click Here"</button> </body> </html> 方法一的代碼明顯比較亂,方法二很多人的寫法都是寫成匿名方法(方法表達式)的形式。 請問在JS的內存泄漏跟JS代碼的規范間大家是怎么平衡的呢?
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 417 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號