Explanation of default parameters in Javascript, and explanation of javascript Parameters
Some languages-like Ruby, CoffeeScript, and the coming javascript version-can declare default parameters when defining a function, as shown below:
Copy codeThe Code is as follows:
Function myFunc (param1, param2 = "second string "){
Console. log (param1, param2 );
}
// Outputs: "first string" and "second string"
MyFunc ("first string ");
// Outputs: "first string" and "second string version 2"
MyFunc ("first string", "second string version 2 ");
Unfortunately, in the current javascript version, this method is invalid. Therefore, what can we do to implement this method and use our existing tool set?
The simplest solution is as follows:
Copy codeThe Code is as follows:
Function myFunc (param1, param2 ){
If (param2 = undefined ){
Param2 = "second string ";
}
Console. log (param1, param2 );
}
// Outputs: "first string" and "second string version 2"
MyFunc ("first string", "second string version 2 ");
The fact is that an unspecified parameter is always "undefined" during access ". If you only have one parameter, this is a good solution. What if there were multiple parameters?
If you have more than one parameter, you can use an object as a parameter. This gives you a clear name for each parameter. If you pass an object parameter, you can declare the default value in the same way.
Copy codeThe Code is as follows:
Function myFunc (paramObject ){
Var defaultParams = {
Param1: "first string ",
Param2: "second string ",
Param3: "third string"
};
Var finalParams = defaultParams;
// We iterate over each property of the paramObject
For (var key in paramObject ){
// If the current property wasn' t inherited, proceed
If (paramObject. hasOwnProperty (key )){
// If the current property is defined,
// Add it to finalParams
If (paramObject [key]! = Undefined ){
FinalParams [key] = paramObject [key];
}
}
}
Console. log (finalParams. param1,
FinalParams. param2,
FinalParams. param3 );
}
MyFunc ({param1: "My own string "});
This is a bit clumsy. If you use this method in many places, you can write an encapsulation function. Fortunately, many libraries now contain related methods, for example, the extend method in jQuery and Underscore.
The following uses the extend method of Underscore to achieve the same result:
Copy codeThe Code is as follows:
Function myFunc (paramObject ){
Var defaultParams = {
Param1: "first string ",
Param2: "second string ",
Param3: "third string"
};
Var finalParams = _. extend (defaultParams, paramObject );
Console. log (finalParams. param1,
FinalParams. param2,
FinalParams. param3 );
}
// Outputs:
// "My own string" and "second string" and "third string"
MyFunc ({param1: "My own string "});
This is how you can get the default parameter in the current javascript version.
Comments are welcome to correct the content.
In javascript, when a function has multiple parameters, how does one "enter only" specific parameters?
In the order of functions, the values before pay can be specified as null (if there is internal verification, enter the default value without errors), and the values after pay can be omitted.
For example:
Var Test = function (arg1, arg2, pay, arg3, arg4 ){...}
Can be called: Test (null, null, 'pay ');
In javascript, when a function has multiple parameters, how does one "enter only" specific parameters?
Enter a space and the program will be executed by default!