开发者

Default values of parametrs for own functions in jquery

开发者 https://www.devze.com 2022-12-23 09:04 出处:网络
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:

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号