Try finally magical, prevent memory leak _javascript skill

Source: Internet
Author: User
Tags finally block
<div id= "d1" ></div> <script > Function Createbutton () {var obj = document.createelement ("button") ; Obj.innerhtml= "point me!"; Obj.onclick=function () {//Processing click event} obj.onmouseover=function () {//Handling MouseOver Event} return obj;//here due to need Back to the object you created, so you cannot set obj directly to null. After return, obj is a local variable and cannot be externally disconnected from its reference to htmlelement. The problem leakage problem in IE will be the "var" button = Document.getelementsbyid ("D1"). AppendChild (Createbu Tton ()); button. do something (); button. do something (); ........ something. Certain things (buttons); ... </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

This type of writing in IE 100% memory leaks

It's easy to solve some problems with try finally
Copy Code code as follows:

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, effectively resolving the issue of the need to set the obj to null after
}
}


In a function or method, there are many places where the return value is required, and the last thing to do

=====================================================================
Attach one: JavaScript Error (try/catch/finally)
Introduction
Like other programming languages, JavaScript provides the possibility.
The try/catch/finally block. Usually when a error is encountered then the script stops and doesn ' t
Continue with the rest of the page. The try/catch/finally block can is used to continue the
Processing with the rest of the page. You just have to put the "code in your" try block and
An error in encountered there, then it'll 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:
<ptml> <pead> </pead> <body> ; script type= "Text/javascript" > try {document.write (unknownvariable)} catch (e) {documen T.write (E.message + "")} finally {document.write ("This was the finally message.")} </script> </body> </ptml>

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.