{"success":true,"name":"removeeventlistener-needs-the-exact-same-function-an-inline-","description":"Removing an event listener silently does nothing, and handlers accumulate","category":"ai-brimind","path":"ai-brimind/removeeventlistener-needs-the-exact-same-function-an-inline-/SKILL.md","file":"SKILL.md","content":"---\nname: removeeventlistener-needs-the-exact-same-function-an-inline-\ndescription: \"Removing an event listener silently does nothing, and handlers accumulate\"\n---\n\n# removeEventListener Needs the Exact Same Function: An Inline Handler Can Never Be Removed\n\n## When to use\nCode adds listeners with inline/anonymous functions and later tries to remove them, or handlers fire multiple times after screens are revisited.\n\n## Example request\naddEventListener with an arrow function, remove with an identical-looking one → two different function objects, removal is a no-op; twin strangers. handler wrapped in bind at add-time → bind created a new function, the original can't unhook it; lost reference. re-entering a screen re-adds the 'same' handler → each visit stacks one more, clicks fire N times; per the cleanup rules, a leak with a trigger. — each tried to cancel a contract using a photocopy.\n\n## Instructions\nAct as a coat-check clerk who returns coats only for the exact ticket issued — a matching description means nothing. Removing a listener works by identity: the browser removes only the very same function object that was added, so two separately written functions with identical code are strangers to it, and removal with a lookalike silently fails — no error, the old handler just lives on. The discipline: any listener that must ever be removed gets a name — store the function in a variable or property at add-time, and pass that same reference to the removal; never add with an inline anonymous function you'll need to unhook. Know the reference-breakers: bind creates a NEW function each call (store the bound result and remove that), and re-created arrow functions in re-run setup code are new objects every time — the classic source of stacking handlers on revisited screens, which is exactly the duplicate-registration failure the cleanup rules forbid. Where supported, the abort-signal option offers the cleaner pattern: add listeners with a controller's signal and remove them all with one abort — no reference bookkeeping at all. Match options too: capture-phase listeners must be removed with the same flag. Verify removal empirically when it matters: trigger the event after removing and confirm silence. Only proceed once every add has a provable path to its remove — same reference, or one shared kill-switch.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #819\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}