I am writing my own jQuery functions. Can I set default values ββfor function parameters?
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 }
I canβt vote right now, but I agree with color2life.
a = a || "something"
probably the shortest and most readable version.
, undefined null.
undefined
null
var f=function(param){ if(param===undefined){ // set default value for param } }
, , a "-"
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);
Source: https://habr.com/ru/post/1736891/More articles:Focus on input field with value - javascriptWhat is the best service / tool for posting short audio clips on a website so that users can click and listen right away? - audioneed help with jquery selector - javascriptHow can I display a PPT file in a Java applet? - javaIs there a timeout for threads waiting for a synchronized method in Java? - javaHow to get * .cer file to add self-signed HTTPS certificate as reliable for Java - javaexcel function to get the value of another cell - excelUsing jQuery to parse RSS feeds having firefox and chrome issues - jqueryPyYAML parsed into an arbitrary object - pythonHow to put jpanel in a dialog box? - javaAll Articles