是否有任何 JS 内置类方法在引用任何方法时只运行一次?
我有一个用于重新加载一些元素的类。
const getMe = async () => await
fetchURL("https://MY.SITE/api/users/me")
.then((res) => res.data);
class loader {
EVERYonceWHENmethodCALLED() {
self.ME = (async () => await getMe())();
}
me() {
$("#me").html(`${self.ME.grade}${self.ME.number}[Tickets: ${self.ME.likeTicket}]`);
} // ** likeTicket depends on ME **
async chart() {
if ($("body").find("#chart").length) {
const musicChart = await fetchURL(`https://MY.SITE/api/music/chart?&gender=${self.ME.gender}`);
$("#chart").html(await listElementsOf(musicChart.data.list));
} // ** listElementsOf depends on ME ( It lets me know what musics I 'liked' to ) **
}
async search(q) {
if ($("body").find("#searchResult").length){
if(q) $("#searchResult").attr("data-q", q);
else q = $("#searchResult").attr("data-q");
const searchResult = await fetchURL(`https://MY.SITE/api/music/search?q=${q}`);
$("#searchResult").html(await listElementsOf(searchResult));
}
}
meAndChart() {
self.me();
self.chart();
}
loadEverything() {
self.meAndChart();
self.search();
}
}
我需要在重新加载任何其他元素之前重新加载 ME,并且我正在寻找一种方法来做到这一点。我可以像 new loader.me();
一样使用 new
,但我不想每次都使用 new
。
有什么办法可以完成预期的工作吗?感谢您的阅读。
I have a class for reloading some elements.
const getMe = async () => await
fetchURL("https://MY.SITE/api/users/me")
.then((res) => res.data);
class loader {
EVERYonceWHENmethodCALLED() {
self.ME = (async () => await getMe())();
}
me() {
$("#me").html(`${self.ME.grade}${self.ME.number}[Tickets: ${self.ME.likeTicket}]`);
} // ** likeTicket depends on ME **
async chart() {
if ($("body").find("#chart").length) {
const musicChart = await fetchURL(`https://MY.SITE/api/music/chart?&gender=${self.ME.gender}`);
$("#chart").html(await listElementsOf(musicChart.data.list));
} // ** listElementsOf depends on ME ( It lets me know what musics I 'liked' to ) **
}
async search(q) {
if ($("body").find("#searchResult").length){
if(q) $("#searchResult").attr("data-q", q);
else q = $("#searchResult").attr("data-q");
const searchResult = await fetchURL(`https://MY.SITE/api/music/search?q=${q}`);
$("#searchResult").html(await listElementsOf(searchResult));
}
}
meAndChart() {
self.me();
self.chart();
}
loadEverything() {
self.meAndChart();
self.search();
}
}
I need to reload ME before reloading any other element, and I'm seeking a way to do this. I can employ new
like new loader.me();
, but I don't want to use new
every time.
Is there any way for the intended job? Thanks for reading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对您的代码做了一些更改:
EVERYonceWHENmethodCALLED
,以便将ME
设置为等待的对象而不是承诺。this
而不是self
(请参阅 此处了解详细信息)。me
和chart
分别重命名为_me
和_chart
。me
和chart
,它们在EVERYonceWHENmethodCALLED
之后调用内部方法。meAndChart
以在EVERYonceWHENmethodCALLED
之后调用内部方法。请注意,使用
await
调用async
函数通常是一个很好的做法(并且通常是必要的),并且这要求调用者函数也是async
>。I made some changes to your code:
EVERYonceWHENmethodCALLED
so thatME
is set to the awaited object rather than a promise.this
instead ofself
for the current object (see here for details).me
andchart
to_me
and_chart
respectively.me
andchart
that call the internal methods afterEVERYonceWHENmethodCALLED
.meAndChart
to call the internal methods afterEVERYonceWHENmethodCALLED
.Note that it is generally a good practice (and often necessary) to call
async
functions withawait
, and that requires the caller functions to be alsoasync
.