[英]How to know if element created with document.createElement still exists
I'm creating an anchor element using document.createElement
in my PhantomJS tests like this: 我在我的PhantomJS测试中使用document.createElement
创建锚元素,如下所示:
var mockLink = null;
beforeEach(function() {
mockLink = document.createElement('a');
});
it('should foo', function() {
// Use mockLink in some way
});
I want to clean up the element in the afterEach
code so that each test will have a newly-created instance of it. 我想清理afterEach
代码中的元素,以便每个测试都具有一个新创建的实例。 Unfortunately, element.remove()
isn't supported in PhantomJS. 不幸的是,PhantomJS不支持element.remove()
。 Source 资源
afterEach(function() {
mockLink.remove(); // Error!
});
And I want to do this without adding the webpage plugin . 我想做到这一点而无需添加网页插件 。
removeChild
is supported by PhantomJS, but my newly-created element doesn't have a parent: PhantomJS支持removeChild
,但是我新创建的元素没有父级:
mockLink.parentElement // undefined
mockLink.parentElement.removeChild(mockLink); // doesn't work
mockLink.parentNode // also undefined
My newly-created link also doesn't seem to be on the document at all. 我新创建的链接似乎也没有出现在文档中。
mockLink.href = 'www.google.com';
document.getElementsByTagName('a'); // doesn't contain google.com
So I can't do 所以我做不到
document.removeChild(mockLink);
mockLink.ownerDocument.contains(mockLink)
also returns false
mockLink.ownerDocument.contains(mockLink)
也返回false
It seems like there are no references to mockLink
stored anywhere besides my mockLink
variable, meaning that I can just set mockLink
equal to null
if I want to let that memory be freed up by garbage collection. 似乎除了我的mockLink
变量之外,没有其他地方可以存储对mockLink
引用,这意味着如果我想通过垃圾回收释放该内存,则可以将mockLink
设置为null
。 But how do I verify this works? 但是,我如何验证此方法有效? If I set any other variable to mockLink
and then console.log
it, it will still come out as defined since it will be a new reference to the same space in memory. 如果我将其他任何变量设置为mockLink
,然后对其进行console.log
,则它仍将按定义出现,因为它将是对内存中相同空间的新引用。 How do I verify that mockLink
is really being deleted when I set the variable to null
? 将变量设置为null
时,如何验证mockLink
确实被删除了?
You've found yourself a conundrum. 您发现自己有一个难题。
If you kill all references to the element by setting mockLink = null
and making sure there are no other references to the element, then the garbage collector should be able to free that object. 如果通过设置mockLink = null
并确保没有其他对该元素的引用来杀死对该元素的所有引用,则垃圾回收器应该能够释放该对象。
But, you cannot verify that because in order to verify that the element is no longer available, you'd have to keep a reference to it, but that would prevent it from getting garbage collected in the first place. 但是,您无法进行验证,因为为了验证该元素不再可用,您必须保留对其的引用,但这将首先阻止它收集垃圾。 Thus, the conundrum. 因此,这个难题。
This is an issue that can't really be measured directly from Javascript itself. 这个问题无法真正直接从Javascript本身来衡量。
If you want to design a one-time test to make sure the memory is being reclaimed, then you can take a memory snapshot, create a several hundred thousand objects stored in an array, clear the array, wait a short time for GC to run, take another memory snapshot, repeat the same process several more times and verify that memory usage of the process is not steadily increasing or study the memory snapshot to make sure none of those objects appear in the memory footprint. 如果要设计一次测试以确保已回收内存,则可以拍摄内存快照,创建存储在数组中的数十万个对象,清除数组,等待一小段时间让GC运行,制作另一个内存快照,重复相同的过程多次并验证该进程的内存使用量是否没有稳定增加或研究内存快照以确保这些对象都没有出现在内存占用量中。
If your javascript environment supports weak references then you can create a weak reference to the node. 如果您的javascript环境支持弱引用,则可以创建对该节点的弱引用。 Depending on the implementation notification mechanisms such as callbacks or reference queues that tell you when it has been collected may also be available. 根据实现的通知机制,例如回调或引用队列,它们可以告诉您何时收集它。
Unprivileged javascript contexts in web browsers currently don't offer such an API. 网络浏览器中的非特权javascript上下文当前不提供此类API。 Node.js, Nashorn and browser addon environments do. Node.js,Nashorn和浏览器插件环境都可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.