浏览器是否执行从未被调用的 JavaScript 函数?
除了增加文件大小之外,这些功能有什么害处吗?
function test() {
// 1000 lines of code (not commented out)
}
浏览器是否执行从未被调用的 JavaScript 函数?
除了增加文件大小之外,这些功能有什么害处吗?
function test() {
// 1000 lines of code (not commented out)
}
只要test()
从不调用,代码就永远不会执行。虽然它可能会被编译。
它可能会解析该代码,但与任何其他函数一样,除非被调用,否则它不会被执行:
test(); // or any of the many other ways to call a function in JavaScript
浏览器是否解析它很可能是依赖于实现的。
浏览器不会执行任何代码,除非在代码中显式引用或在脚本运行期间动态引用。
例子
function func()
{
alert('hello');
}
window.addEventListener('load',func,false);
// With this line it will be called on load.