功能错误,但功能有效 - 为什么?
Errors in function, but the function works - why?
我有一个函数可以生成格式为 "greetingXX.gif"
的随机文件名,其中 "XX"
是 1 到 20 之间的数字。请参见下面的代码:
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)
就像我说的,这个函数完全按照它应该做的去做。但为什么我会收到这些警告?更具体地说,我该如何更改我的代码以免收到它们?
我能够通过使缓存无效来解决这个问题(文件 | 使缓存无效,使缓存无效并重新启动)。为此感谢 lena!
我有一个函数可以生成格式为 "greetingXX.gif"
的随机文件名,其中 "XX"
是 1 到 20 之间的数字。请参见下面的代码:
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)
就像我说的,这个函数完全按照它应该做的去做。但为什么我会收到这些警告?更具体地说,我该如何更改我的代码以免收到它们?
我能够通过使缓存无效来解决这个问题(文件 | 使缓存无效,使缓存无效并重新启动)。为此感谢 lena!