문제

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?

도움이 되었습니까?

해결책

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) {
    // ...
}

다른 팁

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

//noinspection FunctionWithInconsistentReturnsJS
    this.x = function(newX){
...
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top