I have written the following code to implement logging in a separate js file logger.js by using OOP.
var console;
function Logger() {
init();
}
var init = function() {
if(!window.console){
console = {
log: function(message){},
info: function(message){},
warn: function(message){},
error: function(message){}
};
} else {
console = window.console;
}
};
Logger.prototype.log = function(message) {
console.log(message);
}
Logger.prototype.logInfo = function(message) {
console.info(message);
}
Logger.prototype.logWarn = function(message) {
console.warn(message);
}
Logger.prototype.logError = function(message) {
console.error(message);
}
I am using it from another js file, site.js as:
var logger = new Logger(); //global variable
var getComponentById = function(id) {
var component = null;
if(id) {
try {
component = AdfPage.PAGE.findComponentByAbsoluteId(id);
}catch(e){
logger.logError(e);
}
}
return component;
}
I was wondering
Logger
class in proper way, by maintaining OOP of JavaScript.init()
method inaccessible from other js file or method? I mean how can I make it private
?Any pointer would be very helpful to me.
From another SO thread I found information about private method and I changed my approach:
function Logger() {
init();
}
Logger.prototype = (function() {
var console;
var init = function() {
if(!window.console){
this.console = {
log: function(message){},
info: function(message){},
warn: function(message){},
error: function(message){}
};
} else {
this.console = window.console;
}
};
return {
constructor: Logger,
log: function(message) {
this.console.log(message);
},
logInfo: function(message) {
this.console.info(message);
},
logWarn: function(message) {
this.console.warn(message);
},
logError: function(message) {
this.console.error(message);
}
};
})();
But in this case I am getting error that init
is not defined.
To answer your questions:
console
variable with a closure, having it as a property on the Logger makes more sense.I took your code and changed it slightly to come up with this:
// Create the Logger function with an IIFE, this keeps all of the private
// variables out of the global scope, the only thing in the global scope
// is the function returned by the IIFE.
var Logger = (function (w) {
var Logger,
DummyConsole;
DummyConsole = function () {
this.log = function (message) {
alert(message);
};
this.info = function (message) {
// Implement however you want.
};
this.warn = function (message) {
// ...
};
this.error= function (message) {
// ...
};
};
Logger = function () {
if (!w.console) {
this.console = new DummyConsole();
} else {
this.console = w.console;
}
};
Logger.prototype.log = function(message) {
this.console.log(message);
};
Logger.prototype.logInfo = function(message) {
this.console.info(message);
};
Logger.prototype.logWarn = function(message) {
this.console.warn(message);
};
Logger.prototype.logError = function(message) {
this.console.error(message);
};
return Logger;
}(window));
// create a logger instance to check that the Logger class logs to the console.
var a = new Logger();
a.log("hello");
// Remove the console.
window.console = null;
// Create a new logger checking that it falls back to the dummy console implementation.
var b = new Logger();
// An (annoying) alert is shown.
b.log("Hi");
Code is available as a JSFiddle here: http://jsfiddle.net/mtufW/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With