使用try finally很容易解決些問題 復(fù)制代碼 代碼如下:function createButton(){ var obj = document.createElement("button"); " /> 一区二区国产日产,大量国产精品视频,av白虎一区

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

try finally 妙用,防止內(nèi)存泄漏


[Ctrl+A 全選 注:如需引入外部Js需刷新才能執(zhí)行]
這種寫法在IE中100%內(nèi)存泄漏

使用try finally很容易解決些問題
復(fù)制代碼 代碼如下:
function createButton(){ 
 var obj = document.createElement("button"); 
 obj.innerHTML="點(diǎn)我!"; 
 obj.onclick=function(){ 
 //處理click事件 
 } 
 obj.onmouseover=function(){ 
 //處理mouseover事件 
 } 
 try{ 
 return obj; 
 }finally{ 
 obj = null;//這句話在return 之后才執(zhí)行,有效地解決了需在return后將obj置null的問題 
 } 



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

=====================================================================
附一: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需刷新才能執(zhí)行]

JavaScript技術(shù)try finally 妙用,防止內(nèi)存泄漏,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 红安县| 启东市| 依安县| 高平市| 安溪县| 太仆寺旗| 松原市| 靖安县| 林甸县| 从化市| 榕江县| 靖安县| 孙吴县| 安陆市| 通城县| 怀化市| 恩施市| 苍溪县| 米泉市| 沈丘县| 安庆市| 长治市| 怀仁县| 嘉鱼县| 噶尔县| 荆门市| 田东县| 富平县| 翁牛特旗| 灌阳县| 砚山县| 新竹市| 崇仁县| 乌兰浩特市| 化隆| 台北市| 丹凤县| 柘城县| 营山县| 通河县| 湘乡市|