本文介绍了使用WebComponents/ShadowDOM的JavaScript封装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题,我还没有真正找到答案:

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

ShadowDOM或WebComponents(作为更大的Standard)是否也实际上封装了JavaScript?像每个组件都有单独的名称空间一样?

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

根据我在Polymer上遇到的情况,事实并非如此,这很明显,因为Polymer正在通过JS进行填充.

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.

谢谢!

推荐答案

阴影DOM 自定义元素规范对脚本封装一无所知. SD唯一为您提供的是<style>封装和事件重定向(事件看起来像来自主机元素而不是内部元素.对于自定义元素,您可以执行技巧为JS命名空间(例如匿名自调用函数,将构造函数包装在命名空间中,等等)

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.)

这篇关于使用WebComponents/ShadowDOM的JavaScript封装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:10