Update the function itself to potentially assign a value to str
var DBProcessing = function(str)
{
str = str || 'ok';
console.log(str);
...
}
Javascript differs from strongly typed languages in that it does not require specific method signatures based on argument types and quantities.
In traditional strongly typed languages, method overloading would be implemented as follows:
public void OverloadedMethod()
{
}
public void OverloadedMethod(int id)
{
}
public void OverloadedMethod(int id, int age)
{
}
This strict approach mandates precise definitions of arguments for each method.
In javascript, there are no explicit method declarations; instead, only one method signature is typically defined, which can accept varying types and quantities of inputs. The handling of these inputs is then left up to the coder within the method implementation.
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
If additional arguments are passed, they can still be accessed using the arguments object:
function JSMethod(id, age){
console.log(id);
console.log(age);
console.log(arguments[2]);
}
JSMethod(1, 26, 99);
//outputs 1, 26, 99
Despite this flexibility, it is advisable to clearly define a method's purpose and parameters whenever possible. Even though javascript methods can handle variable numbers and types of arguments, setting default values is recommended for clarity and consistency.
function JSMethod(id, age){
id = id || 1;
age = age || 16;
}