简体   繁体   English

从JavaScript文件中删除未使用的函数

[英]Remove unused functions from JavaScript file

My JavaScript code is very large even after compression. 即使经过压缩,我的JavaScript代码也很大。 How can I find all unused functions so I can remove them? 如何找到所有未使用的功能,以便将其删除?

For example in CSS files to remove unused classes I can use the "Dust-Me" Firefox addon. 例如,在CSS文件中删除未使用的类,我可以使用“ Dust-Me” Firefox插件。 Is there a way to do the same for JavaScript? 有没有办法对JavaScript执行相同的操作?

I'm not sure if you are referring to Java or Javascript, but my wild guess is it must be Javascript since you mention things like JQuery and CSS. 我不确定您是指Java还是Javascript,但我大为猜测是它一定是Javascript,因为您提到了JQuery和CSS之类的东西。 So please fix your title and question description if it is wrong. 因此,如果错误,请修正您的标题和问题描述。

Anyway, if you are looking for a way to list down all javascript functions, please take a look at this: How to list all of javascript functions beginning with _func 无论如何,如果您正在寻找一种列出所有JavaScript函数的方法,请看一下: 如何列出以_func开头的所有JavaScript函数

You can just ignore the 'func' filtering. 您可以忽略“ func”过滤。
eg 例如
if(typeof window[x] === "function") functions.push(x);

Although we still need to find a way to filter it. 尽管我们仍然需要找到一种过滤方法。

Another way is to just have an array that will mark if a function is being called. 另一种方法是仅具有一个数组,该数组将标记是否正在调用函数。
eg 例如

usedFunctions = array();
function myFunc(){    
    usedFunctions.push('myFunc');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM