I know global variables are generally considered bad practice.
(我知道全局变量通常被认为是不好的做法。)
However, I do not want to be dogmatic. (但是,我不想教条。)
Consider the following situation: (请考虑以下情况:)
- I need to check the availability of different rooms at different dates and times.
(我需要检查不同日期和时间的不同房间的可用性。)
- The reservation system offers a weekly overview for every room.
(预订系统会提供每个房间的每周概览。)
- I have a two dimensional array of my reservation needs, grouped by weeks in order to minimize HTTP requests.
(我有一个二维的预订需求数组,按周分组,以最大程度地减少HTTP请求。)
- For every group, I initiate a
fetch()
request. (对于每个组,我都会发起一个fetch()
请求。)
If it is successful, I parse the server's response and check whether the desired time slots for every reservation in the current group are free or not. (如果成功,我将解析服务器的响应,并检查当前组中每个预留的所需时隙是否空闲。)
As the fetch()
calls are all fired very fast and run asynchronously, the function initiating them will finish quickly and way before all responses are there.
(由于fetch()
调用都非常快地触发并异步运行,因此启动它们的函数将很快完成,并且不会出现所有响应。)
Thus, I consider using two global variables in order to keep track of all the reservations: one containing the reservations and one containing the number of requests to be sent (this number is known at the beginning). (因此,我考虑使用两个全局变量来跟踪所有保留:一个包含保留,另一个包含要发送的请求数(此数字开头是已知的)。)
Every time a slot is checked, the reservations could be easily updated and the count reduced by one. (每次检查插槽时,都可以轻松地更新预留,并将计数减少一。)
As soon as there are no more responses left to be received, the script knows that the reservations are in their final state and can generate its output. (一旦没有更多要接收的响应,脚本便知道预留处于最终状态,并可以生成其输出。)
Why am I considering global variables?
(为什么要考虑全局变量?)
- It avoids passing the same data over and over again.
(这样可以避免一遍又一遍地传递相同的数据。)
People often say that global variables have bad performance. (人们经常说全局变量的性能很差。)
However, passing data multiple times seems to be inefficient as well. (但是,多次传递数据似乎也没有效率。)
Also, performance is not an issue, because this is just a Tampermonkey script running locally, processing roughly 30 reservations every few months. (另外,性能也不是问题,因为这只是在本地运行的Tampermonkey脚本,每几个月大约处理30个预订。)
Currently, I check the reservations manually and spend more time than even the slowest script could possibly take. (目前,我手动检查预订,花费的时间甚至比最慢的脚本可能花费的时间还要多。)
- The global variable can be made semi-global in the sense that it would not be accessible from outside the Tampermonkey script.
(在无法从Tampermonkey脚本外部访问的意义上,可以将全局变量设置为半全局变量。)
- I could group all the
fetch()
promises and pass them to Promise.all
. (我可以将所有fetch()
承诺分组,然后将它们传递给Promise.all
。)
However, I think this is not an option, because I still need to wait for all other requests, even after one has failed. (但是,我认为这不是一个选择,因为即使在一个请求失败之后,我仍然需要等待所有其他请求。)
- I could use
async
/ await
instead of .then()
chaining. (我可以使用async
/ await
代替.then()
链接。)
But then the fetch()
requests would not run in parallel anymore. (但是,然后fetch()
请求将不再并行运行。)
As performance is not an issue, this would not be too bad. (由于性能不是问题,所以这不会太糟。)
However, it seems not very elegant to me. (但是,对我来说似乎不是很优雅。)
This leads me to my question: Should global variables in Javascript be avoided at all costs or are they a viable approach in a situation like the one I have just described?
(这引出了我的问题:是否应该不惜一切代价避免使用Javascript中的全局变量,或者在像我刚才描述的那样的情况下,它们是可行的方法吗?)
ask by Philipp Imhof translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…