Reputation: 478
As per hoisting function expressions cannot be invoked before they are defined. I have created a module where I am trying to access the IIFE functions before the IIFE is defined. According to my understanding it should have given an error saying cookieManager.setPerstistentCookie is not a function but it works fine.Why?
$(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
}
})();
Upvotes: 0
Views: 42
Reputation: 478
Sorry guys my bad I was right about hoisting it is not working and it should not working as i interpreted.
The reason it was some how working was because I had the same script snippet copied above the $(function(){})
snippet which I forgot to remove by mistake.
Any ways thank you for the help.
Upvotes: 0
Reputation: 413826
You've wrapped the function that refers to cookieManager
in a jQuery "ready" handler. That code won't run until the DOM is ready, so by that time the object is defined. You're not using it before it's defined, in other words.
Upvotes: 2