Drilled-prog
@Drilled-prog
Программирую на ['php', 'js']

Как лучше сделать цепочку вызовов if-elseif-else со скрытием elseif-else по умолчанию?

Так вроде бы работает, только по умолчанию надо скрыть методы elseif, else. может как то лучше можно сделать ?
class BaseLol {
  #isNextIf = false;

  public if(condition: boolean) {
    if (condition) {
      this.#isNextIf = false;
    } else {
      this.#isNextIf = true;
    }
    return this as Omit<this, "if">;
  }

  public elseif(condition: boolean) {
    if (this.#isNextIf && condition) {
      this.#isNextIf = false;
    }
    return this;
  }

  public else() {
    if (this.#isNextIf) {
      this.#isNextIf = false;
    }
    return this as Omit<this, "else" | "elseif">;
  }
}
  • Вопрос задан
  • 122 просмотра
Пригласить эксперта
Ответы на вопрос 1
Adamos
@Adamos
if (condition) {
      this.#isNextIf = false;
} else {
      this.#isNextIf = true;
}
// this.#isNextIf = !condition;

if (this.#isNextIf && condition) {
      this.#isNextIf = false;
}
// this.#isNextIf &= !condition;

if (this.#isNextIf) {
      this.#isNextIf = false;
}
// this.#isNextIf = false;
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы