将DOM节点与“ ===”进行比较是否安全? [英] Is it safe to compare DOM nodes with "==="?
问题描述
我的前提条件是,我只使用一个DOM,并且仅使用DOM API,例如 getElementById
或 querySelector
或事件
引用。在这种情况下,使用以下条件是否安全?
My prerequisite is, that I work with just one DOM and use just the DOM API like getElementById
or querySelector
or event
references. Is it in this case safe to use the following condition
if (document.body === event.currentTarget) { }
检查事件是否已冒泡到文档正文中?
to check if the event has been bubbled to the body of the document?
我知道这不适用于克隆的对象。但是我不会自己克隆任何对象。 DOM API会在任何地方克隆对象吗?
I know that this will not work with cloned objects. But I do not clone any objects by myself. Does the the DOM API clone objects anywhere?
推荐答案
是的,这很安全。 DOM对象不会被浏览器替换,它们是相当稳定的对象。
Yes, this is safe. DOM objects aren't replaced by the browser, they're fairly stable objects.
我本人经常使用WeakMaps将数据绑定到元素而不会出现问题(示例)。
I myself frequently use WeakMaps to bind data to elements without problems (example).
这篇关于将DOM节点与“ ===”进行比较是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!