国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

try finally 妙用,防止內存泄漏

2019-11-21 01:47:38
字體:
來源:轉載
供稿:網友

[Ctrl+A 全選 注:如需引入外部Js需刷新才能執行]

這種寫法在IE中100%內存泄漏

使用try finally很容易解決些問題
復制代碼 代碼如下:

function createButton(){ 
 var obj = document.createElement("button"); 
 obj.innerHTML="點我!"; 
 obj.onclick=function(){ 
 //處理click事件 
 } 
 obj.onmouseover=function(){ 
 //處理mouseover事件 
 } 
 try{ 
 return obj; 
 }finally{ 
 obj = null;//這句話在return 之后才執行,有效地解決了需在return后將obj置null的問題 
 } 



一個函數或方法中,其實有很多地方都需要這種選返回值,最后執行某些事的

=====================================================================
附一:JavaScript Error (try/catch/finally)
Introduction
Like other programming languages, JavaScript provides the possibility to make use of 
the try/catch/finally block. Usually when an error is encountered then the script stops and doesn't
 continue with the rest of the page. The try/catch/finally block can be used to continue the 
processing with the rest of the page. You just have to put the code in your try block and when 
an error in encountered there, then it will call the catch block. The finally block is called always 
regardless of an error occurred or not. The following example makes the usage clear.

Example: 

[Ctrl+A 全選 注:如需引入外部Js需刷新才能執行]
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汨罗市| 东台市| 扶余县| 高尔夫| 焦作市| 常熟市| 连江县| 措美县| 中山市| 施甸县| 台北县| 皋兰县| 丹阳市| 南溪县| 清河县| 监利县| 乐山市| 盐亭县| 鹿邑县| 天峨县| 焦作市| 吐鲁番市| 河西区| 绩溪县| 新兴县| 绥宁县| 辰溪县| 连州市| 景宁| 稷山县| 江山市| 灵宝市| 孟村| 盐亭县| 九寨沟县| 抚顺县| 武城县| 东安县| 海口市| 许昌县| 东台市|