Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Encapsulation with WebComponents/ShadowDOM

Probably simple question, that I haven't really found an answer for:

Does ShadowDOM or WebComponents (as the bigger Standard) actually encapsulate JavaScript aswell? Like having seperate namespaces for each component?

From what I encountered with Polymer that wasn't the case yet, which is obvious, given that polymer is doing it's polyfilling through JS.

Thanks!

like image 802
AlessandroEmm Avatar asked Feb 15 '23 04:02

AlessandroEmm


1 Answers

The Shadow DOM and Custom Element specs say nothing about script encapsulation. The only thing that SD gives you is <style> encapsulation and event retargeting (events look like they come from the host element and not internal elements. For custom elements, you can do tricks to namespace your JS (e.g. anonymous self calling functions, wrapping the constructor in a namespace, etc.)

like image 172
ebidel Avatar answered Feb 17 '23 19:02

ebidel