Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript returned object properties returned by reference or value?
    primarykey
    data
    text
    <p>I have the below code from a tutorial on creating a firefox extension.</p> <p>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.</p> <pre><code>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&lt;il; i++) { elm = allLinks[i]; if (elm.getAttribute("target")) { elm.className += ((elm.className.length &gt; 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); </code></pre> <p>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?</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload