JSFiddle - React, Tailwind, and code Playground
HTML
<!-- PLEASE OPEN YOUR JAVASCRIPT CONSOLE! -->
<!--Organize Your Console Logging With a Custom debug() Function -->
<!-- blog.kevinchisholm.com -->
<!-- Example # 3A -->
JavaScript
var
//show debug messages
debugMode = false,
myDebug = function(msg,callback){
//if app not in debug mode, exit immediately
if(!debugMode || !console){return};
//console.log the message
if(msg && (typeof msg === 'string')){console.log(msg)};
//execute the callback if one was passed-in
if(callback && (callback instanceof Function)){
callback();
};
},
//objects to merge
obj1 = {
name: 'alfred e newman',
phone: '212-555-1212'
},
obj2 = {
account: '010203',
address: '404 Mad Ave. - MadCity NY, 12345'
},
//array for testing
myArr = ['monday','tuesday','wednesday','thursday','friday'],
//combines two objects
combineObjects = function(objA,objB){
var
newObj = {},
merge = function(baseObj,addObj){
for(var prop in addObj){
if(addObj.hasOwnProperty(prop)){
baseObj[prop] = addObj[prop];
};
};
};
merge(newObj,objA);
merge(newObj,objB);
//debug message
myDebug('the new combined object is: ',function(){
console.dir(newObj);
});
return newObj;
},
//converts an array of strings to upper-case
arrayToUpper = function(arr){
//code that does something with arr
for(var i = 0; i < arr.length; i++){
arr[i] = arr[i].toUpperCase();
//debug message
myDebug( ('the new string for element # ' + (i + 1) + ' is: ' + arr[i]) );
};
}
//call the functions
var foo = combineObjects(obj1,obj2)
arrayToUpper(myArr);
//added for proof-of-concept
console.dir(foo);
console.dir(myArr);