0
我从下面的代码中创建一个Firefox扩展教程。Javascript返回的对象属性通过引用或值返回?
它使用创建一个对象并分配它的方法,我之前没有遇到过,而且有些代码让我感到困惑,但我理解JS如何合理地工作。
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);
我的问题是,你在自执行函数中创建了变量prefManager。然后返回要分配给变量linkTargetFinder的对象。 在返回对象之前将变量置于方法定义中时,该变量是否转换为其值。还是继续引用自我执行函数中创建的变量,我认为这个函数会在函数返回后被销毁或至少超出范围?
非常感谢您提供任何帮助,我已经尝试过几次实验,看看我能否以这种或那种方式证明它,并且周围环境很好,尽管我不确定要搜索什么。