In my social contract, I taked about using JSLint.
There were two problems reported with the current release of Operator that were very strange when I heard about them. One was that Firefox didn’t honor the “Show the tab bar when only one tab is open” preference anymore and the other was that external links opened in new windows. I spent a bit of time debugging the problem and tracked it down to this code:
disable: function()
{
toolbar = document.getElementById("operator-toolbar");
toolbarbuttons = toolbar.getElementsByTagName("toolbarbutton")
for(var i=toolbarbuttons.length - 1; i>=0; i--) {
if (toolbarbuttons[i].id != "operator-options") {
toolbarbuttons[i].setAttribute("disabled", "true");
toolbarbuttons[i].label = toolbarbuttons[i].getAttribute("origlabel");
toolbarbuttons[i].setAttribute("label", toolbarbuttons[i].label);
}
}
},
Do you see the problem there? Because I didn’t have “var” in front of toolbar, I was affecting the global variable toolbar which was causing all the problems that people were seeing. Anytime an extension uses a variable with a common name without putting “var” in front of it, it risks overwriting other global variables in the browser.
Running JSLint through my code with the “Detect undefined variables” option would have found this problem and other problems as well. When I used JSLint, I found many cases where I had done this and I was able to easily find and fix them all.
Some of the other things I was able to fix as a result of JSLint included missing semicolons, unnecessary global variables, using == where I should use ===, as well as general cleanup. One error in particular (Weird construction. Delete ‘new’.) encouraged me to reevaluate some code I had written and I was able to clean it up considerably.
So I would encourage you to at least attempt to run JSLint against your extension to make sure you aren’t creating any side effects you don’t intend.
Note that there is one construct that JSLint complains about that is very common in Mozillla/Firefox source code:
Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Components.interfaces.nsIPromptService);
JSLint would prefer you wrote it like this (note the location of the period)
Components.classes["@mozilla.org/embedcomp/prompt-service;1"].
getService(Components.interfaces.nsIPromptService);
I went ahead and made this change in my code to avoid seeing the JSLint errors, but your mileage may very.