Memory Leak on element.onclick

瀏覽次數:30 次
跳到第一則未讀訊息

Mohit Kanika

未讀,
2020年5月1日 晚上11:41:382020/5/1
收件者:Zakas Books
Hi Nicholas,

From your book "PROFESSIONAL JAVASCRIPT FOR WEB DEVELOPERS third edition", on page 227(Memory Leaks). You have provided below pattern of code to avoid memory leaks. 
Could you please confirm that below pattern is required in Chrome or Firefox where DOM is implemented as JS object and Mark & sweep is used as GC? i.e. do I need to always set element to null after I assign event handlers on an element?

    function assignHandler() {
        var element = document.getElementById("someElement");
        var id = element.id;
        element.onclick = function () {
            alert(id);
        };
        element = null;
    }


Nicholas Zakas

未讀,
2020年5月5日 上午11:00:102020/5/5
收件者:zakas...@googlegroups.com
I think so but I’m not 100% sure. Browsers now have devtools that allow you to inspect memory, so I’d suggest using that to verify (these didn’t exist when the book was written).

--
You received this message because you are subscribed to the Google Groups "Zakas Books" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zakasbooks+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/zakasbooks/8c0ec4ae-bf69-43d4-b8f3-5f46850fca48%40googlegroups.com.

Mohit Kanika

未讀,
2020年5月6日 晚上9:20:532020/5/6
收件者:Zakas Books
Thank you Nicholas, will look into devtool option.


On Tuesday, May 5, 2020 at 11:00:10 AM UTC-4, Nicholas C. Zakas wrote:
I think so but I’m not 100% sure. Browsers now have devtools that allow you to inspect memory, so I’d suggest using that to verify (these didn’t exist when the book was written).
On Fri, May 1, 2020 at 8:41 PM Mohit Kanika <kanik...@gmail.com> wrote:
Hi Nicholas,

From your book "PROFESSIONAL JAVASCRIPT FOR WEB DEVELOPERS third edition", on page 227(Memory Leaks). You have provided below pattern of code to avoid memory leaks. 
Could you please confirm that below pattern is required in Chrome or Firefox where DOM is implemented as JS object and Mark & sweep is used as GC? i.e. do I need to always set element to null after I assign event handlers on an element?

    function assignHandler() {
        var element = document.getElementById("someElement");
        var id = element.id;
        element.onclick = function () {
            alert(id);
        };
        element = null;
    }


--
You received this message because you are subscribed to the Google Groups "Zakas Books" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zakas...@googlegroups.com.
回覆所有人
回覆作者
轉寄
0 則新訊息