首页 > 解决方案 > 功能错误,但功能有效 - 为什么?

问题描述

我有一个函数可以生成一个随机文件名,格式为 1 到 20 之间的数字。请参见下面的代码"greetingXX.gif""XX"

 1 function getGIF(callback) {
 2    let randomGIF;
 3    let gifName = "greeting";
 4    const GIF_EXTENSION = ".gif";
 5    const MAX_GREETING = 20;
 6    randomGIF = Math.floor(Math.random() * MAX_GREETING) + 1;
 7    if (randomGIF < 10) {
 8       gifName += "0" + randomGIF;
 9    } else {
10       gifName += randomGIF;
11    }
12    gifName += GIF_EXTENSION;
13    callback(gifName);
14 }

该功能有效,但在 WebStorm 中我收到以下警告:

Unused Variable randomGIF (Line 2)
Unused constant MAX_GREETING (Line 5)
Element MAX_GREETING is not imported (Line 6)
Variable gifName might not have been initialised (Line 8 and Line 10)

就像我说的那样,该函数完全按照它应该做的事情。但为什么我会收到这些警告?更具体地说,我如何更改我的代码,以免我得到它们?

标签: node.jswebstorm

解决方案


我能够通过使缓存无效(文件|使缓存无效、无效并重新启动)来解决此问题。感谢莉娜


推荐阅读