删除外部js文件后会发生什么

发布于 2024-10-22 07:47:45 字数 373 浏览 1 评论 0原文

<!-- Add-in device.js files are placed here -->
    <div id="deviceScript"></div>

我一直在做的是,当我选择一个新的(插件)设备并附加一个脚本标记及其 .src 文件时,请清除此问题。

我有一些模糊的想法,取消外部 js 文件可能会从内存中删除其代码。如果没有的话有什么办法吗?

不同脚本文件中的函数具有相同的名称 - 例如,我使用 start() 来设置每个设备。似乎最新的设备 start() 会覆盖内存中的设备 - 但我不确定覆盖是否意味着删除最后一个设备,或者是否存在一些可怕的渣滓堆积。

任何澄清表示赞赏。

<!-- Add-in device.js files are placed here -->
    <div id="deviceScript"></div>

What I have been doing is clearing this out when I select a new (add-in) device and append a script tag with its .src file.

I have some vague idea that unhooking an external js file might remove its code from memory. If not is there a way to do so?

Functions in different script files have the same name - for example, I use start() to set up each device. It seems that the latest device start() overwrites the one in memory - but I am not sure if overwrite means delete last one or whether there is some horrendous build up of dross going on.

Any clarification appreciated.

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

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

发布评论

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

评论(2

狼亦尘 2024-10-29 07:47:45

如果您删除 JavaScript 文件并且网页已加载,则代码仍然在内存中(至少浏览器可以访问它),因此您仍然可以“使用”它。如果您重新加载/刷新页面,代码将无法访问,因此您将无法使用它。我认为没有办法从内存中(或到目前为止浏览器可以访问的任何地方)删除这段代码。

如果您希望在某个页面中使用所有函数,那么在多个 js 文件中编写相同名称的函数并不是一个好习惯。将使用最后声明的 js 中定义的函数,因此如果您调用该函数两次(在您的情况下为 start() ),您的 function() 将执行相同的操作。

如果在同一个 js 上向两个函数写入相同的名称,其工作原理类似。虽然在其他语言中,您可能将 myfunc(oneparm) 和 myfunc(parmone, parmtwo) 作为两个独立的函数,其中一个函数根据传递的参数数量运行,但在 JavaScript 中,最后定义的函数将始终是运行的函数,无论什么情况参数的数量。

请记住 javascript 是一种客户端脚本语言

If you remove the javascript file and the webpage is already loaded, the code is still in memory (at least the browser can access to it) so you can still 'use' it. If you reload/refresh the page the code will not be accessible and hence you will cannot use it. I think that there is no way of deleting this code from memory (or from wherever it is accessible for the browser so far).

It is not a good practice to write the same name of a function in several js files if you wish to use them all in a certain page. The function defined in the last stated js would be used, so if you call to that function twice (start() in your case) your function() will do the very same.

In case of writing the same name to two functions on the same js, it works similarly. Whilst in other languages you might have myfunc(oneparm) and myfunc(parmone, parmtwo) as two separate functions with the one that gets run depending on the number of parameters passed, in JavaScript the last one defined will always be the one run regardless of the number of parameters.

Keep in mind javascript is a client-side scripting language

青衫儰鉨ミ守葔 2024-10-29 07:47:45

我认为这可能是一个答案,但也许这实际上是另一个问题 - 这符合逻辑吗?

上下文:访问者选择一个命名对象来显示一组键(假设其中一个是“颜色”)。选择某个键会显示一个或多个设备名称(例如 setColor)。选择设备名称会显示一组选项(例如红色、绿色、蓝色)。

在第二步中,我将以前的 device.js 替换为新的 device.js。在选择选项调用其中的函数来执行操作之前,js 有时间加载。

因此,如果任何函数名称与之前的名称重复,它们会因为交换操作而覆盖它们。这意味着我不必担心命名约定,除了主函数的名称与设备名称相同之外......设备可以由任何人制作。也许时不时地重新加载是个好主意。

有什么想法吗?谢谢

Here's what I think might be an answer but perhaps it is really another question - is this logical?

The context: Visitor selects a named object to display a set of keys (say one is Color). Selection of a key displays one or more device names (say setColor). Selection of a device name displays a set of options (say Red, Green, Blue).

In the second step I swap the previous device.js with a new device.js. The js has time to load before the selection of the option calls a function within it to make the action.

So if any of the function names duplicates what has gone before, they overwrite them because of the swap action. This means I don't have to worry about naming conventions other than the name of the main function is the same as the device name ... devices could be made by anyone. Perhaps it would be a good idea to reload every now and again.

Any thoughts? Thanks

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