Modify the function itelf to conditionally set str
var DBProcessing = function(str)
{
str = str || 'ok';
console.log(str);
...
}
Javascript is unlike strongly typed languages in which you must provide concrete method signatures with respect to the number and type of arguments.
In traditional strongly typed languages, you would implement method overloading like
public void OverloadedMethod()
{
}
public void OverloadedMethod(int id)
{
}
public void OverloadedMethod(int id, int age)
{
}
in which you must be very specific with regards to the number and type of arguments.
In javascript, there are no such method declarations and you can only really declare one method signature with the method accepting a variable type/number of inputs. It is up to you in the method itself how these arguments are handled.
function JSMethod(id, age){
console.log(id);
console.log(age);
}
JSMethod();
//outputs 'undefined', 'undefined'
JSMethod(1);
//outputs 1, 'undefined'
JSMethod(1, 26);
//outputs 1, 26
Ok, what if you passed a third argument? You can do
function JSMethod(id, age){
console.log(id);
console.log(age);
console.log(arguments[2]);
}
JSMethod(1, 26, 99);
//outputs 1, 26, 99
This is because each method has an arguments object containing all the passed parameters the callee provided.
However, best practice dictates that you make your methods intention as clear as possible. So, although a javascript method can accept a variable type/number of arguments, it is better that they are stated and set to default values if need
function JSMethod(id, age){
id = id || 1;
age = age || 16;
}
var DBProcessing2 = function(str), of check parameter type inside