I have the below code from a tutorial on creating a firefox extension.
It uses a method of creating an object and assigning it that I have not come across before and some of the code has me confused as I though that I understood how JS worked reasonably well.
var linkTargetFinder = function () {
var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
return {
init : function () {
gBrowser.addEventListener("load", function () {
var autoRun = prefManager.getBoolPref("extensions.linktargetfinder.autorun");
if (autoRun) {
linkTargetFinder.run();
}
}, false);
},
run : function () {
var head = content.document.getElementsByTagName("head")[0],
style = content.document.getElementById("link-target-finder-style"),
allLinks = content.document.getElementsByTagName("a"),
foundLinks = 0;
if (!style) {
style = content.document.createElement("link");
style.id = "link-target-finder-style";
style.type = "text/css";
style.rel = "stylesheet";
style.href = "chrome://linktargetfinder/skin/skin.css";
head.appendChild(style);
}
for (var i=0, il=allLinks.length; i<il; i++) {
elm = allLinks[i];
if (elm.getAttribute("target")) {
elm.className += ((elm.className.length > 0)? " " : "") + "link-target-finder-selected";
foundLinks++;
}
}
if (foundLinks === 0) {
alert("No links found with a target attribute");
}
else {
alert("Found " + foundLinks + " links with a target attribute");
}
}
};
}();
window.addEventListener("load", linkTargetFinder.init, false);
My question is, where you have the variable prefManager created in the self executing function. That then returns the object to assign to the variable linkTargetFinder. Is the variable converted to its value when it is placed into the method definition before the object is returned. Or does it continue to reference the variable created in the self executing function which I thought would be destroyed or at least out of scope after the function had returned?
Thank you very much in advance for any help, I have tried several experiments to see if I can prove it one way or another and also had a good look around, though I am kind of unsure what to search for.