function foo(a, opt_b) {
opt_b = opt_b || 1;
...
}
foo(1); // IntelliJ will yell at me, saying "Invalid number of parameters, expected 2"
Is there a way to document foo()
such that IntelliJ won't yell at me?
According to http://usejsdoc.org/tags-param.html there are two ways to use comments to declare a parameter as optional:
/**
* @param {string} [somebody] - Somebody's name.
*/
function sayHello(somebody) {
if (!somebody) {
somebody = 'John Doe';
}
alert('Hello ' + somebody);
}
and
/**
* @param {string=} somebody - Somebody's name.
*/
function sayHello(somebody) {
if (!somebody) {
somebody = 'John Doe';
}
alert('Hello ' + somebody);
}
Both ways worked for me.
Using JavaDoc-style comments for JavaScript, you can declare a parameter as optional with brackets [] around it:
/**
* My function
* @param [optionalParam] An optional parameter
*/
myFunction: function(optionalParam) {
//...
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With