Try finally magical to prevent memory leaks

Source: Internet
Author: User

Try finally magical to prevent memory leaks

<div id= "D1" ></div>
<script >
function Createbutton () {
var obj = document.createelement ("button");
Obj.innerhtml= "point me!";
Obj.onclick=function () {
Handling the Click event
}
Obj.onmouseover=function () {
Handling MouseOver Events
}
Return obj;//you cannot set obj directly to NULL because the object you created needs to be returned. After return, obj is a local variable and cannot be externally disconnected from its htmlelement reference. There will be a problem leaking problem in IE
}
The var button = Document.getelementsbyid ("D1"). AppendChild (Createbutton ());
button. do something ();
button. do something ();
........
something. Certain things (buttons);
......
</script>


This type of writing in IE 100% memory leaks

It's easy to solve some problems with try finally
function Createbutton () {
var obj = document.createelement ("button");
Obj.innerhtml= "point me!";
Obj.onclick=function () {
Handling the Click event
}
Obj.onmouseover=function () {
Handling MouseOver Events
}
try{
return obj;
}finally{
obj = null;//This sentence is executed after return, which solves the problem of having the obj null after the
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.