I normally put my JavaScript function/classes/other things in namespaces. Namespacing your JavaScript is good, it avoids conflicts between scripts on the page and keeps the global namespace clean. I could go into more detail, but luckily it has been done for me.
Declaring namespaces, and checking they exist before using them is a bit of a pain. You might find yourself doing something like:
if(!window.com) window.com = {};
if(!com.mycompany) com.mycompany = {};
if(!com.mycompany.mypackage) com.mycompany.mypackage = {};
…and then add things to it like:
com.mycompany.mypackage.myfunction = function() { return 138; };
To make things worse, if your webpage includes a different script that uses slightly different namespace, say "com.mycompany.mypackage2" you’ll have to check again that the objects "com" and "com.mycompany" already exist.
The following function allows you to use a namespace, without having to manually go through the throws of checking each "space" exists and creating an empty object for each level.
/**
* I declare a namespace if it doesn't already exist.
*
* @param {String} ns The namespace to create/use
* @param {Object} parent (Optional) The parent object onto which this namespace will be attached.
* @return True if the namespace was created successfully.
* @type Boolean
*/
function namespace(ns, parent) {
if(!parent) {
parent = window;
} else {
// Parent must be an object
if(typeof(parent) != 'object') {
return false;
}
}
var names = ns.split('.');
if(!names.length) {
return true;
}
var name = names[0];
if(!parent[name]) {
parent[name] = {};
}
if(names.length > 1) {
names.shift();
return namespace(names.join('.'), parent[name]);
}
return true;
}
…and is used like so:
namespace('com.mycompany.mypackage');
com.mycompany.mypackage.myfunction = function() { return 138; };
https://shareyoursocial.com/madelyn541
https://app.brancher.ai/user/mmFZcjXGgjkE
https://793657.8b.io/
https://mirfali.com/author/effectiverte10/
http://andreas-dominic.org/author/collgr503/
https://profile.sampo.ru/madelyn541
https://huckerreport.com/author/lyricsdyk1004/
https://reduser.net/members/madelyn541.241630/#about
http://hurricanesurf.net/author/sistericb19/
https://fora.babinet.cz/profile.php?section=personality&id=150750
https://interestpin.com/user/137998
http://riotstories.co.uk/author/streampra888/
https://buildingchurch.tv/author/cameroonlfz453/
https://schoolido.lu/user/madelyn541/
https://www.ruhrpott-poker.de/author/provelkv2883/
https://animeforums.net/profile/78899-madelyn541/
https://hutsons-hacks.info/author/campingila0823/
https://kaeuchi.jp/forums/users/madelyn541/
https://www.mymeetbook.com/madelyn541
https://www.lemongrovelane.com/author/designedkdr239/
https://md.sebastians.dev/s/k30e-DWEg
https://pixelandbloom.com/author/functionouh7611/
https://www.ebooklingo.com/profile/16163
https://theresanaiforthat.com/@madelyn541/
http://nursesoncall.com/author/improvedpnz962/
http://vetstate.ru/forum/?PAGE_NAME=profile_view&UID=301463
https://payhip.com/b/RhCXF
http://www.grecal.net/author/defelw42/
https://www.mateball.com/madelyn541
https://stadiontusanj.ba/author/proceduredqj568/
http://www.stes.tyc.edu.tw/xoops/modules/profile/userinfo.php?uid=4078067
https://www.freelistingaustralia.com/listings/poppers
https://www.democracylab.org/user/68865
https://www.varni.ae/author/grilloud580/
https://www.betting-forum.com/members/madelyn541.189399/#about
https://www.hostboard.com/forums/members/madelyn541.html
https://jazz.org.au/author/strandoci97/
https://divinguniverse.com/user/madelyn541
https://bandori.party/user/1843498/madelyn541/
http://www.halalflash.com/author/studiesrgg208/
https://app.wedonthavetime.org/profile/madelyn541
https://kumu.io/madelyn541/ujytgsfvrdesw
https://yunesrealty.com/author/examinedlqw96/
https://www.pexels.com/@madelyn-me-2164876664/
http://www.rainmaker.in.th/author/rosesmfc38/
https://www.walkaholic.me/ca/user/69674-madelyn541
https://safechat.com/u/madelyn541
http://www.galacticpotatoes.com/author/naturallyfed07/
https://www.intensedebate.com/people/madelyn541
http://thelifeofmissy.com/author/rssbid4332/