S
S
Sergey Erzhovich2022-03-01 17:55:14
OOP
Sergey Erzhovich, 2022-03-01 17:55:14

What's the best way to chain if-elseif-else calls with elseif-else hidden by default?

So it seems to work, only by default you need to hide the elseif, else methods. how can it be done better?

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">;
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adamos, 2022-03-01
@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;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question