gpt4 book ai didi

javascript - 如何在嵌套 IIFE 模块中引用父 JavaScript 函数?

转载 作者:行者123 更新时间:2023-11-28 19:47:20 24 4
gpt4 key购买 nike

采用下面的代码...在setTimeout中匿名函数,引用alert.hide()的正确方法是什么?方法?将整个调用写为 admin.alert.hide(); 是否正确? ?或者有没有更好的方法引用admin无需直接调用它?

var admin = (function(jQuery, window, document, undefined) {
return {
loader : (function(admin) {
var fade = 75;
var loader = '#loader';
return {
show : function () {
jQuery(loader).stop().fadeIn(fade);
},
hide : function() {
jQuery(loader).stop().fadeOut(fade);
}
}
})(),
alert : (function() {
var timeout;
var fade = 500;
var milliseconds = 1000;
var alert = '#alert';
return {
timeout : timeout,
show : function(message) {
jQuery(alert).find('p').text(message);
jQuery(alert).stop().fadeIn(fade);
clearTimeout(this.timeout);
this.timeout = setTimeout(function() { }, milliseconds);
},
hide : function() {
jQuery(alert).stop().fadeOut(fade);
}
}
})()
}
})(jQuery, window, document);

最佳答案

您可以执行以下操作:

return 
{
timeout : timeout,
show : function(message)
{
jQuery(alert).find('p').text(message);
jQuery(alert).stop().fadeIn(fade);
clearTimeout(this.timeout);
this.timeout = setTimeout((function() { this.hide(); }).bind(this), milliseconds);
},
hide : function()
{
jQuery(alert).stop().fadeOut(fade);
}
}

关于javascript - 如何在嵌套 IIFE 模块中引用父 JavaScript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24063088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com