Skip to content

Instantly share code, notes, and snippets.

@beckettkev
Last active June 17, 2016 15:50
Show Gist options
  • Save beckettkev/efa6b62d5b08fc31bc53f76331c35502 to your computer and use it in GitHub Desktop.
Save beckettkev/efa6b62d5b08fc31bc53f76331c35502 to your computer and use it in GitHub Desktop.
/*
With the example below, we safely check to see if the object exists already before creating one (if it does exist, use this).
e.g. var SharePointCookies = SharePointCookies if it exists else create a new empty JavaScript object
This approach safely checks to make sure each portion of the namespace exists before creating it (to not write over pre-existing code)
*/
var SharePointCookies = SharePointCookies || {};
SharePointCookies.NameSpaces = SharePointCookies.NameSpaces || {};
SharePointCookies.NameSpaces.Example = SharePointCookies.NameSpaces.Example || {};
/*
Now if you were to try and do this with a more direct approach...
The issue with using a more direct approach, is that SharePointCookies may already exist but
SharePointCookies.NameSpaces and SharePointCookies.Example do not. So if SharePointCookies did exist, in this following scenario, they
will not be created (with this in mind if you could have written it like this..).
This is why we should not do the following...
*/
var SharePointCookies = SharePointCookies || { NameSpaces: { Example: {} } };
/*
So with the view that the initial example is safer,
you could then use this namespace to store some variables and functions...
*/
var SharePointCookies = SharePointCookies || {};
SharePointCookies.NameSpaces = SharePointCookies.NameSpaces || {};
SharePointCookies.NameSpaces.Example = SharePointCookies.NameSpaces.Example || {};
SharePointCookies.NameSpaces.Example = function() {
var output, legacy;
function log(msg) {
console.log(msg);
}
return {
Log: function(msg) {
if (typeof console.log !== 'undefined') {
log(msg);
}
}
};
};
/*
A better approach...
We can use the first example and encapuslate an anonymous function with our namespace like the following
*/
var SharePointCookies = SharePointCookies || {};
SharePointCookies.NameSpaces = SharePointCookies.NameSpaces || {};
(function(namespace) {
var output = '';
var legacy = true;
function formatter(s) {
return '\n' + s + ' [Length:' + s.length + ']';
}
function getJsonObject(obj, index, spacing) {
var indent = spacing || '';
if (typeof console.table !== 'undefined' && !legacy) {
console.table(obj);
} else {
Object.keys(obj).forEach(function(key, i) {
i = typeof index !== 'undefined' ? i + index : i;
if (typeof obj[key] === 'object') {
getJsonObject(obj[key], i, indent + ' ');
} else {
output += formatter(indent + key + ': ' + obj[key]);
}
});
}
}
// private function
function log(content) {
// super private - no access to this variable outside this function
var size;
if (Array.isArray(content)) {
content.forEach(function(msg) {
console.log('%c ' + formatter(msg), namespace.Style);
});
} else if (typeof content === 'string') {
console.log('%c ' + formatter(content), namespace.Style);
} else if (typeof content === 'object') {
getJsonObject(content);
console.log('%c ' + output, namespace.Style);
output = '';
}
}
// anything we add to namespace is publicly accesssible under SharePointCookies.NameSpaces.Example
namespace.Log = function Log(msg, style) {
if (typeof console.log !== 'undefined') {
namespace.Style = style;
log(msg);
}
};
})(SharePointCookies.NameSpaces.Example = SharePointCookies.NameSpaces.Example || {});
console.clear();
console.log('\n\nExample #1 - Call our publicly exposed Log function with a string...');
SharePointCookies.NameSpaces.Example.Log('Cheese on toast is great', 'background-color:blue;color:#ffffff');
console.log('\n\nExample #2 - Call our publicly exposed Log function with an array...');
SharePointCookies.NameSpaces.Example.Log(['blue is a great colour','orange is a lovely colour','yellow reminds me of bananas'], 'color:blue');
console.log('\n\nExample #3 - Call our publicly exposed Log function with a JSON object');
SharePointCookies.NameSpaces.Example.Log({cheese:'toast', banana: { sliced: 'essential', heated: { optional: true, tempreture:'200 degrees'} }}, 'background-color:black;font-size: 2em; color:pink');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment