I'm trying to create my own class for logging errors and messages to a database, but I'm new to creating custom classes and methods in Javascript. After plenty of research, I was able to put together the following code:
function log ()
{
}
log.error = function (currentFunction, logMessage)
{
createLog (currentFunction, logMessage, "Error");
}
And I'm able to call the log.error function successfully with:
log.error("current function name", "this is my error message");
My question is this: Most of the sites I looked at said to organize your code like this instead:
function log ()
{
this.error = function (currentFunction, logMessage)
{
createLog (currentFunction, logMessage, "Error");
}
}
But whenever I try to call that error function, I get the following error:
Cannot find function error in object function log(currentFunction, logMessage)
Does anyone know why this is? For the sake of keeping my code clean, I would really like to be able to place everything inside one function, but at this point it looks like I may have to separate everything out, as in the first code block above.
EDIT - MY SOLUTION
Thanks to both dystroy and Jose Vega (sorry Jose, I don't have enough reputation to up-vote your answer). I ended up using dystroy's solution simply because I feel it will be easier for me to maintain my code in the future, however Jose's answer helped give me insight into what I was trying to do and the different options available to me. I feel I have a much better grasp on what's going on when you do something like var objectName = {} vs function className () {}.
Here's my new code:
function log ()
{
log.prototype.error = function (currentFunction, logMessage)
{
createLog (currentFunction, logMessage, "Error");
}
log.prototype.warning = function (currentFunction, logMessage)
{
createLog (currentFunction, logMessage, "Warning");
}
log.prototype.info = function (currentFunction, logMessage)
{
createLog (currentFunction, logMessage, "Info");
}
function createLog (currentFunction, logMessage, messageType)
{
//code that updates MSSQL database
}
}