Default values of parametrs for own functions in jquery
I am writing my own jQuery functions. Is it possible to set开发者_运维知识库 the default values of the function parameters?
One way to do this is to check the parameters. For example:
function test(paramA) {
if (paramA == null) {
paramA = defaultValue;
}
// use paramA here
}
Another possibility is this:
function test() {
var paramA = defaultValue;
if (arguments.length == 1) {
paramA = arguments[0];
}
// use paramA here
}
i can't vote right now but agree with color2life.
a = a || "something"
is probably the most concise and readable version.
You might want to check for undefined
instead of null
.
var f=function(param){
if(param===undefined){
// set default value for param
}
}
if not defined then a have "something"
a = a || "something"; // setting default value
(function($) {
$.fn.yourFunction= function(opts) {
var options = $.extend({}, $.fn.yourFunction.defaults, opts);
....
$.fn.yourFunction.defaults = {
param1: "someval",
param2: "",
param3: "#ffffff"
};
})(jQuery);
精彩评论