Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I tell IntelliJ (10.5) not to warn about not passing an optional javascript method argument?

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?

like image 462
ripper234 Avatar asked Feb 27 '12 12:02

ripper234


2 Answers

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.

like image 88
Mark Ritterhoff Avatar answered Oct 01 '22 14:10

Mark Ritterhoff


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) {
    //...
},
like image 20
entreprenr Avatar answered Oct 01 '22 14:10

entreprenr