Re: [WICG/webcomponents] HTML Modules: JavaScript Scoping of ShadowDom Children (Issue #959)

From @Jamesernator in #645:
> > This would alleviate under-utilization of inline JavaScript as it is right now. Being able to use `on*=` attributes, and others, while referencing the included class would be incredibly useful.
> 
> This sort've concept is interesting, but web specs don't add features that have special powers to be able to do things in other JS scopes. Like in order to resolve `this.#handleClick` somehow `shadowRoot.appendChild(template.content.cloneNode(true))` would have to be able to look into the constructor of `this` to find private fields, this isn't a capability the language offers.
> 
> A general rule of thumb, is that web objects don't really have more power than JS objects, they are just (potentially) implemented in a different language. In this case there would be no way to implement something like what you were describing:
> 
> ```js
> class ShadowRoot extends DocumentFragment {
>     appendChild(node) {
>         if (node.hasAttribute("onclick")) {
>             // No way to access this.#handleClick in this method
>             // so web specs can't either
>         }
>     }
> }
> ```
> 
> The exact reasons for this general rule of thumb are varied, some reasons include the ability to separate the JS engine from the browser, some are to allow the engine to optimize things it otherwise couldn't, some are just to respect the intended design of features (i.e. private means private), some are to allow JS to faithfully mock APIs in tests, etc.

@Jamesernator made a good point about private members. However, I still feel that this may still be viable for public members.

-- 
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/issues/959#issuecomment-1367003971
You are receiving this because you are subscribed to this thread.

Message ID: <WICG/webcomponents/issues/959/1367003971@github.com>

Received on Thursday, 29 December 2022 00:49:42 UTC