使用try finally很容易解決些問題 復制代碼 代碼如下:function createButton(){ var obj = document.createElement("button"); " /> 国产精品毛片视频,国产精品亚洲二区,亚洲狠狠婷婷综合久久久

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

try finally 妙用,防止內存泄漏


[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需刷新才能執行]

JavaScript技術try finally 妙用,防止內存泄漏,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 雷山县| 枞阳县| 郁南县| 那坡县| 曲麻莱县| 互助| 麻阳| 湖北省| 北宁市| 临夏市| 孟州市| 襄汾县| 罗甸县| 那曲县| 聊城市| 巫山县| 阿拉尔市| 晋州市| 翁牛特旗| 玉环县| 惠州市| 徐汇区| 修武县| 甘德县| 大竹县| 江北区| 聂荣县| 宝山区| 辰溪县| 寻乌县| 昌平区| 荆州市| 于田县| 英山县| 巫山县| 潜江市| 长沙市| 汶上县| 嘉祥县| 灵宝市| 伊金霍洛旗|