Mozilla extension namespace: How to differentiate between my function and functions from other packages?
I am developing a Firefox (FF) extension and am calling a function when a particular button on FF is pressed. Sometimes, FF is not able to find my function from my script file even if it is there. Instead, it searches for it in the browser extension and throws an error.
In general, how do I use namespace before my function that will clearly differentiate it from functions of perhaps the same name in other packages.
For e.g.: Is there something like:
<script type="application/javascript" src="chrome://mythird/content/script.js"/>
<statusbar id="status-bar">
<statusbarpanel id="mythird-statusbarpanel" label="mythird" onclick="chrome://mythird/myalert();"/>
</statusbar>
instead of just:
<statusbarpanel id="mythird-st开发者_高级运维atusbarpanel" label="mythird" onclick="myalert();"/>
You can use a normal object as namespace of your functions. Give the object the name of your extension (which should be unique and awesome) and you should be pretty save.
In your script file:
var gYourExtensionName = {};
gYourExtensionName.myalert = function(){/*...*/};
and in XUL:
<script type="application/javascript" src="chrome://mythird/content/script.js"/>
<statusbar id="status-bar">
<statusbarpanel id="mythird-statusbarpanel"
label="mythird"
onclick="gYourExtensionName.myalert()" />
</statusbar>
You should also read: Firefox Extensions: Global Namespace Pollution
精彩评论