What lie beneath “useWeakReference”
I just read the most surprise secret of addEventListener workflow, wrote by Ted. He explained the process if we have use the default value of ‘useWeakReference’ ( fault by default ). The garbage collector will not remove its in almost situation. This result can be raise more unexpected performance for our application.
In the other hand, if we set this argument with ‘true’ value, the garbage collector will found its, then remove it as fast as it’s possible (this will not break common rule of object-oriented programming language)
You can read more about Ted’s exposing here.
ความจริงที่น่าตกใจก็คือ เมื่อใดก็ตามที่พวกเรา ได้ทำการ addEventListener แล้วไม่ได้ set ค่าอะไรให้กับ argument ที่ชื่อ useWeakReference (ซึ่งค่า default ของมันคือ false) นั่นหมายความว่า instance ของ object ที่เรานำมา subscribe หรือในอีกทางหนึ่ง เรียกว่า การนำ functionของ instance นั้นมา เป็น listener นั่นเอง ดังตัวอย่างด้านล่าง
1: this.addEventListener( Event.EventName, foo.fooFunction );
อย่างนี้ล่ะครับที่จะมีการ garbage collect ใดๆ ทั้งสิ้น แต่ถ้าหากเราเปลี่ยนเป็น
1: this.addEventListener( Event.EventName, foo.fooFunction , true );
อย่างนี้ล่ะครับนาย garbage collector จะเฝ้ามอง reference ของ foo จากมุมมืด มีโอกาสเมื่อไหร่จัดการทันที
No related posts.



about 5 months ago
I highly enjoyed reading your article, keep on creating such exciting stuff.