Default values ​​of parameters for native functions in jquery

I am writing my own jQuery functions. Can I set default values ​​for function parameters?

+3
source share
5 answers

One way to do this is to check the parameters. For instance:

function test(paramA) {
    if (paramA == null) {
        paramA = defaultValue;
    }
    // use paramA here
}

Another possibility is:

function test() {
    var paramA = defaultValue;
    if (arguments.length == 1) {
        paramA = arguments[0];
    }
    // use paramA here
}
+9
source

I can’t vote right now, but I agree with color2life.

a = a || "something"

probably the shortest and most readable version.

+3
source

, undefined null.

var f=function(param){
  if(param===undefined){
    // set default value for param
  }
}
+2

, , a "-"

a = a || "something"; // setting default value
0
(function($) {
    $.fn.yourFunction= function(opts) {
        var options = $.extend({}, $.fn.yourFunction.defaults, opts);

        ....

        $.fn.yourFunction.defaults = {
        param1: "someval",
        param2: "",
        param3: "#ffffff"
    };

})(jQuery);
0

Source: https://habr.com/ru/post/1736891/


All Articles