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; };
http://drrock.com/author/steelxaa22/
http://cscharling.dk/author/manoraug01/
http://squacre.com/author/horsetmg659/
https://monkeyhousegames.com/author/finishncc517/
http://www.naviloop.com/author/fiscalzxe59/
https://foodiescancook.com/author/exactbvk09/
https://www.sosyalciniz.net/author/frozenkqv517/
https://skilltecs.com/author/placingfng092/
https://mirfali.com/author/pcieqg89/
https://autism-toolkit.ie/author/alfredgvh48/
https://www.ruhrpott-poker.de/author/youxhc0020/
https://skilltecs.com/author/exclusivedeg61/
https://mathemagiciansclub.com/author/expectationssvl410/
https://www.varni.ae/author/homepagezjf0855/
http://thelifeofmissy.com/author/distributedhzn97/
http://brandconti.com/author/boulevardofk41/
https://medibulletin.com/author/newbietww0259/
http://www.galacticpotatoes.com/author/squadvjs5535/
http://www.tabletwetestates.es/author/correctedqbt4874/
http://nubried.net/author/savingsyym23/
http://brandconti.com/author/screeningeml4231/
http://pipeband.org.nz/author/interpretednom3661/
http://squacre.com/author/karaokednm531/
https://pixabay.com/users/u_f6mmilfcu8-58008064/
https://www.sosyalciniz.net/author/warhyc143/
https://www.lissubito.com/author/establishmentnfn98/
https://app.readthedocs.org/profiles/madelyn541/
https://loriwhitlock.com/author/infectionsty7405/
https://manylink.co/@madelyn541
https://uvirtual.edu.co/author/palestinemll5578/
https://www.iniuria.us/forum/member.php?732755-madelyn541
http://bobbysuperstarsensation.com/author/spokegdy75/
https://www.xosothantai.com/members/madelyn541.656327/
https://expathealthseoul.com/profile/madelyn-me/
https://www.akaqa.com/question/q19192751582-Gde-kupit-poppers-online
https://foodiescancook.com/author/guitarxtu144/
https://pastelink.net/z826nsxi
https://geraldfries.de/author/definingxzb210/
http://spacedecisions.com/author/platformsfkp7308/
https://learningapps.org/display?v=pn62emgck26
https://velog.io/@madelyn541/poppers
https://www.skypixel.com/users/djiuser-4gyvqnrrrpu4-c4078e1f45a3
https://www.skypixel.com/users/djiuser-4gyvqnrrrpu4-c4078e1f45a3
https://pastebin.com/u/madelyn541
https://runtrip.jp/users/859461
https://allmyfaves.com/madelyn541
https://www.bandsworksconcerts.info/index.php?gdsgdsfg
https://md.coredump.ch/s/Z87YDdlbh
https://www.fanart-central.net/user/madelyn541/profile
https://www.heroscapers.com/members/madelyn541.768959/#about