Build javascript function to accept default values
function myFunct(){
//blah blah blah
}
how to build a function with key/value pair parameters so when i call this, it will be called 开发者_开发知识库like this?
myFunt(prm1:'value1',prm2:'value2',prm3:'value3');
so, when i only need to call the 3rd param, i will do this:
myFunct(prm3:'value3');
Specify some defaults in your function and then call using only the params you need:
function myFunct(param) {
var prm1 = param.prm1 || "default1";
var prm2 = param.prm2 || "default2";
var prm3 = param.prm3 || "default3";
}
Call it using a param object, like this:
myFunct({prm3:'value3'});
prm1
and prm2
will get the default values, but prm3
will get what you passed.
You can specify any combination of values in your param object. Any you leave out will be populated with their default values.
If you're using jQuery you can make this a little prettier using $.extend
:
function myFunct(param) {
var parameters = $.extend(true, /* deep copy */
{prm1: "default1", prm2: "default2", prm3: "default3"}, param);
};
The first object given to extend
will serve as the default and the properties in your param
object will be merged in when present.
function myFunt(jsonObj){
var param3 = jsonObj.prm3;
}
Call the function like this:
myFunt({prm3: 'value3'});
As far as I know, that is not supported by Javascript. You can however achieve a similar effect by just passing one argument, that is an object.
Call:
foo({ prm1: 'value1', prm2: 'value2', prm3: 'value3'})
Function definition:
function foo(args)
{
//use values accordingly
var prm1 = args.prm1;
}
Javascript doesn't directly support this syntax (named parameters, specifically), so you'll have to resort to some sort of workaround. There are two approaches that work in certain situations:
If you only need certain contiguous subsets of the parameters supplied, you can just declare them in order and then manually check whether the remaining parameters have been supplied. Javascript lets you call a function with less than the number of declared parameters, with the unpassed ones defaulting to
undefined
. Hence you could do something like this:function myFunc(prm3 ,prm1, prm1) { // Use defaults if not supplied if (typeOf(prm1) == 'undefined') prm1 = 'value1'; if (typeOf(prm2) == 'undefined') prm2 = 'value2'; // Rest of function as normal ... };
Alternatively, if you need more flexibility (i.e. either
prm3
orprm2
could be supplied on their own, you'll need some way of associating a name with the value. Hence you'd have to pass all parameters in as an associate array, which is javascript is simply an object:function myFunc(prms) { // Unpack actual arguments - if not supplied, will be 'undefined' var prm1 = prms.prm1; var prm2 = prms.prm2; var prm3 = prms.prm3; // Rest of function as normal ... }; // Call this function something like the following: myFunc({prm1: 'Hello', prm3: 'World'});
Now both of these approaches have disavantages, but if you need to support optional arguments they're the only ways I'm aware of to do it.
Use arrays.
either:
var param = [null, 'value2', null]
or
var param = ['value1', 'value2', 'value3']
with function:
myFunct(param);
function myFunct(array){
//blah blah blah
}
This is most easily done by passing an object in as an argument:
myFunct({prm1: 'value', prm2: 'value', prm3: 'value'});
However, if you want any omitted key to have a default value, the most common methodology for this is to use $.extend
(assuming you are using jQuery). Example:
function myFunct(obj) {
var defaults = {
prm1: 'value',
prm2: 'value',
prm3: 'value'
};
// Set any defaults
obj = $.extend(defaults, obj);
// Output the results to the console
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
console.log("obj[" + i + "] =", obj[i]);
}
}
}
Then you can call some sample code:
myFunct(); // will output all three params as being "value"
myFunct({prm3: 'test'}); // will output first two as being "value", third as being "test"
If you are not using jQuery, you can use the method described by lwburk above. However, lwburk's method gets rather lengthy if you have a lot of options possible.
The reason the extend method works is that it takes the first object (default
in this case) and overwrites all values in the first object (default
) with the ones specified in the second object (obj
in this case). So $.extend({a: 1, b: 2}, {a: 2})
returns {a: 2, b: 2}
; note that the a
value was taken from the second object, but the b
value from the first was untouched because it was not specified in the second.
Other libraries have similar extend
methods (not an extensive list):
- jQuery's extend (as linked above)
- Prototype's extend
- MooTools's merge
Or you could write your own, or use the code from any of the above.
精彩评论