Frage

Let's say I have this Class:

function Point(x,y){
  var _x=x,_y=y;
  this.x = function(newX){
    if (typeof newX !== 'undefined') {
      //function x() working as a setter
      _x=newX;
    }
    else {
      //function x() working as a getter
      return _x;
    }
  }
}

PHPStorm complains about the function (x()) having inconsistent return points. I found that adding return undefined; just after _x=newX; solves the whine and the code looks like this.

function Point(x,y){
  var _x=x,_y=y;
  this.x = function(newX){
    if (typeof newX !== 'undefined') {
      //function x() working as a setter
      _x=newX;
      return undefined;
    }
    else {
      //function x() working as a getter
      return _x;
    }
  }
}

The question is: Is there a way to make PHPStorm stop whining about this?

War es hilfreich?

Lösung

Another option would be to annotate the methods return type properly, so that PhpStorm knows that the function is expected to return undefined in some cases.

/**
 * @param {number} [newX]
 * @returns {number|undefined}
 */
this.x = function(newX) {
    // ...
}

Andere Tipps

you can suppress the warning for this function only, like:

//noinspection FunctionWithInconsistentReturnsJS
    this.x = function(newX){
...
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top