我可以从浏览器内存中删除xml文件吗?

时间:2021-12-27 04:14:46

I am working on a javascript web app. It progresses through "pages" and loads an xml file for each page. Is there a way to remove an xml file from memory?

我正在开发一个javascript网络应用程序。它通过“页面”进行并为每个页面加载一个xml文件。有没有办法从内存中删除xml文件?

Or do I even need to? Is having say 20 xml files in memory bad?

或者我甚至需要?在内存中说20个xml文件坏了吗?

1 个解决方案

#1


1  

You didn't really give enough details about your problem, so I will assume that you mean you have XML files as strings in memory. In that case, you can use the delete keyword for that.

你没有真正提供有关你的问题的足够细节,所以我假设你的意思是你有XML文件作为内存中的字符串。在这种情况下,您可以使用delete关键字。

However, according to the Mozilla Developer documentation, "You can use the delete operator to delete variables declared implicitly but not those declared with the var statement."

但是,根据Mozilla Developer文档,“您可以使用delete运算符删除隐式声明的变量,但不能删除使用var语句声明的变量。”

This means you'll have to add properties to the window object itself instead:

这意味着您必须向窗口对象本身添加属性:

> var a = "Lorem...";
undefined
> delete a;
false
> a;
"Lorem..."
> window.b = "Lorem...";
"Lorem..."
> delete window.b;
true
> b
ReferenceError: b is not defined

Another way to write this is to declare variables implicitly, without the var keyword. This also works:

写这个的另一种方法是隐式声明变量,而不使用var关键字。这也有效:

> c = "Lorem...";
"Lorem..."
> delete c;
true

Since JavaScript is garbage collected, when these become unreferenced they are cleared from memory automatically.

由于JavaScript是垃圾收集的,当它们被取消引用时,它们会自动从内存中清除。

#1


1  

You didn't really give enough details about your problem, so I will assume that you mean you have XML files as strings in memory. In that case, you can use the delete keyword for that.

你没有真正提供有关你的问题的足够细节,所以我假设你的意思是你有XML文件作为内存中的字符串。在这种情况下,您可以使用delete关键字。

However, according to the Mozilla Developer documentation, "You can use the delete operator to delete variables declared implicitly but not those declared with the var statement."

但是,根据Mozilla Developer文档,“您可以使用delete运算符删除隐式声明的变量,但不能删除使用var语句声明的变量。”

This means you'll have to add properties to the window object itself instead:

这意味着您必须向窗口对象本身添加属性:

> var a = "Lorem...";
undefined
> delete a;
false
> a;
"Lorem..."
> window.b = "Lorem...";
"Lorem..."
> delete window.b;
true
> b
ReferenceError: b is not defined

Another way to write this is to declare variables implicitly, without the var keyword. This also works:

写这个的另一种方法是隐式声明变量,而不使用var关键字。这也有效:

> c = "Lorem...";
"Lorem..."
> delete c;
true

Since JavaScript is garbage collected, when these become unreferenced they are cleared from memory automatically.

由于JavaScript是垃圾收集的,当它们被取消引用时,它们会自动从内存中清除。