最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2008年03月31日 18:55:55   作者:  
預(yù)防內(nèi)存泄漏

[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]

這種寫(xiě)法在IE中100%內(nèi)存泄漏

使用try finally很容易解決些問(wèn)題
復(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的問(wèn)題 
 } 



一個(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需再刷新一下頁(yè)面才能執(zhí)行]

相關(guān)文章

最新評(píng)論

年辖:市辖区| 遵义县| 江孜县| 鸡东县| 淮北市| 滨海县| 巴林左旗| 张家港市| 芮城县| 绥德县| 洪雅县| 东源县| 宕昌县| 伊宁市| 乌兰浩特市| 兰考县| 屯昌县| 陈巴尔虎旗| 壶关县| 通江县| 佳木斯市| 吴桥县| 三亚市| 中阳县| 达日县| 略阳县| 福安市| 白河县| 抚远县| 贡觉县| 镇江市| 茂名市| 禹州市| 多伦县| 林州市| 辽源市| 监利县| 松江区| 桐柏县| 华蓥市| 丁青县|