gpt4 book ai didi

javascript - 能够在定义之前使用函数表达式

转载 作者:行者123 更新时间:2023-12-02 14:38:48 25 4
gpt4 key购买 nike

根据提升函数表达式,在定义之前不能调用它们。我创建了一个模块,在定义 IIFE 之前尝试访问 IIFE 函数。根据我的理解,它应该给出一个错误,说 cookieManager.setPerstistentCookie 不是一个函数,但它工作正常。为什么?

$(function(){

var selectedContainerClassName = $('.mtaa-iml-dropdwn-options li.mtaa-iml-selected').data('parent');
cookieManager.setPersistentCookie(cookieManager.getCookieNameByComponent('imlookingTo') , selectedContainerClassName );

});

var cookieManager = (function(){

var cookieEnum = {
imlookingTo : "selectedUIContainer"
}

function getPerstistentCookieExpiryTime(){
var expiration_date = new Date();
expiration_date.setFullYear(expiration_date.getFullYear() + 1);
return expiration_date.toGMTString();
}

var setPersistentCookie = function(cname, cvalue) {
var expires = "expires="+ getPerstistentCookieExpiryTime();
document.cookie = cname + "=" + cvalue + "; " + expires;
}



return {
setPersistentCookie : setPersistentCookie,
getCookieNameByComponent : getCookieNameByComponent
}

})();

最佳答案

您已将引用 cookieManager 的函数包装在 jQuery“就绪”处理程序中。在 DOM 准备好之前,该代码不会运行,因此到那时对象就已定义。换句话说,在它被定义之前你不会使用它。

关于javascript - 能够在定义之前使用函数表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37201337/

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