javascript - How best for a Firefox extension to avoid polluting the global namespace? -



javascript - How best for a Firefox extension to avoid polluting the global namespace? -

i've been reading on global namespace pollution when developing extension firefox, , want avoid much possible in extension. there several solutions, generally, solutions seem center around declaring 1 global variable extension, , putting in that. add together 1 variable global namespace, isn't bad.

as brief aside, have had solution proposed me avoids putting any variables global namespace; wrap in function. problem here there's nil refer in xul overlays. have declare elements in overlays, , in js add together ton of addeventlisteners replace would've been oncommand="..." in xul. don't want this; want xul include events in xul because think looks cleaner, isn't solution me. hence need @ to the lowest degree 1 global variable xul oncommand="..." attributes refer to.

so consensus seems to have 1 (and one) variable extension, , set code within that. here's problem: generally, people recommend that variable named nice long, unique name have 0 chance of colliding other variables. if extension's id myextension@mycompany.com, name variable myextensionatmycompanydotcom, or com.mycompany.myextension. avoiding collisions in global namespace, there's 1 problem; variable name long , unwieldy. xul going littered references event handlers along lines of oncommand="myextensionatmycompanydotcom.dosomeevent". there's no way avoid having refer global namespace in xul overlays, because overlay gets added browser window's dom; doesn't have namespace of own, can't somehow limit our extension's variable scope our own overlays. so, see it, there 4 solutions:

1. utilize long variable name in xul

this results in rather unwieldy, verbose xul code like:

<statusbarpanel id="mystatusbar" onmousedown="myextensionatmycompanydotcom.onmystatusbarclick();"> 2. add together element of randomness short variable name

we come much nicer short variable name our extension, let's myext, , add together random characters on create unique, such myextax8t9. in xul, have:

<statusbarpanel id="mystatusbar" onmousedown="myextax8t9.onmystatusbarclick();">

clearly, results in rather ugly , confusing code random characters odd, , create kind of temporary variable.

3. don't declare global variables @ all

you wrap in functions. this, of course, means there nil refer in xul, , every event must attached xul elements using addeventlistener in javascript code. don't solution because, mentioned above, think it's cleaner have events referenced in xul code rather having search ton of js code find events attached xul elements.

4. utilize short variable name in xul

i phone call extension's variable myext, , nice xul code like:

<statusbarpanel id="mystatusbar" onmousedown="myext.onmystatusbarclick();">

of course, short name much more clash else in global namespace , isn't ideal.

so, have missed something? there alternative 4 solutions proposed above? if not, best of 4 (given #3 unacceptable me), , why?

we utilize javascript module pattern described in blog post: http://www.adequatelygood.com/2010/3/javascript-module-pattern-in-depth. can export symbols want utilize in xul handlers described.

in addition, utilize reversed host name module name prefix ensure command namespace:

/* set global namespace. */ if (typeof(com) == "undefined") var com = {}; if (!com.salsitasoft) com.salsitasoft = {}; /* main namespace. */ com.salsitasoft.myextensionglobalstuffgoeshere = (function (my) { homecoming my; })(com.salsitasoft.myextensionglobalstuffgoeshere || {});

update: changed pass com.salsitasoft.myextensionglobalstuffgoeshere closure if exists namespace can spread across multiple files.

javascript firefox namespaces firefox-addon jsm

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -