有没有办法继承javascript Node接口?

发布于 2024-12-17 10:21:52 字数 749 浏览 0 评论 0原文

我想编写一些从 DOM Node 接口继承的 javascript 类...听起来很像 如何从-dom-element-class继承,但我不想在我的 [X|XHT|HT]ML 文档中使用它的实例。我的目标是构建一个绝对独立的 DOM 树,所以我想使用 现代浏览器提供的 Node 接口,而不是创建我自己的 DOM 实现。节点不应具有任何 HTML 属性,例如类名、样式或(客户端)偏移量 - 只是一个普通的 Node。该类不需要可扩展,Node 方法就足够了。

如果有可能创建一个 DocumentFragment 或一个新文档,我也会很高兴
a) 不是 (X)HTML 文档而是 XML 文档并且
b) 与当前文档不关联(如 document.implementation 所示)

该脚本应该可以在最新版本的 Opera 和 Firefox 中运行。其他浏览器是可有可无的,而不是必须的;而且我不关心任何旧版本。

I want to write some javascript classes which inherit from the DOM Node interface... Sounds very much like how-to-inherit-from-the-dom-element-class, but I do not want to use its instances it in my [X|XHT|HT]ML document. My aim is to build a absolutely stand-alone DOM tree, so I would like to use the Node interface provided by modern browsers instead of creating my own DOM implementation. The nodes should not have any HTML attributes like classname, styles or (client)offsets - just a plain Node. The class doesn't need to be extensible, the Node methods would be enough.

I would also be happy if there was a possibility to create a DocumentFragment or a new Document which
a) is not a (X)HTML document but a XML document and
b) is not associated with the current document (as in document.implementation)

The script should work in the latest versions of Opera and Firefox. Other browsers are a nice-to-have, not a must; and I don't care about any old versions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

软的没边 2024-12-24 10:21:52

看来这是最接近我的要求了:

new DOMParser().parseFromString("<root />", "text/xml");

但是仍然没有办法继承Node接口。

It seems to be that this is the closest to my requirements:

new DOMParser().parseFromString("<root />", "text/xml");

But still no way to inherit from the Node interface.

攒一口袋星星 2024-12-24 10:21:52

不。 Node 是一个抽象接口。它没有构造函数操作,这意味着你既不能构造它也不能继承它。它只是抛出

接口对象,其 接口 未使用 构造函数操作在调用时会抛出异常,无论是作为函数还是作为 构造函数

因此,虽然您可以编写 class Sub extends Node {},但您永远无法实例化它。同样,从 Node.prototype 继承是可能的,但毫无意义,因为所有内置方法都需要内部字段。

No. Node is an abstract interface. It does not have a constructor operation, which means that you can neither construct it nor inherit from it. It just throws:

Interface objects whose interfaces are not declared with a constructor operation will throw when called, both as a function and as a constructor.

So while you can write class Sub extends Node {}, you could never instantiate it. Similarly, inheriting from Node.prototype is possible but pointless since all the builtin methods require the internal fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文