How are Bootstrap JS instances stored in DOM elements? #42058
-
|
When you call It's just for curiosity. I couldn't figure that out searching through the source code. Also, what happens to the instance when you remove the element from the DOM? Is it garbage-collected? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Bootstrap 5 uses a nested
When you call Garbage collection — the important partNo, instances are NOT automatically garbage-collected when you remove the element from the DOM. Since Bootstrap uses a regular If you remove an element without calling const modal = bootstrap.Modal.getInstance(element);
if (modal) modal.dispose();
element.remove();
Why not WeakMap? A |
Beta Was this translation helpful? Give feedback.
Bootstrap 5 uses a nested
Mapstructure in its internal Data module (src/dom/data.js). Here's how it works:"bs.modal","bs.tooltip")When you call
Modal.getInstance(element), it looks up the element in the outer Map, then retrieves the"bs.modal"entry from the inner Map.getOrCreateInstancedoes the same but creates a new instance if one doesn't exist.Garbage collection — the important part
No, instances are NOT automatically garbage-collected when you remove the element from the DOM. …