-5
\$\begingroup\$

I have the following parent class method in JavaScript:

class Parent {
    jump() {
        if (!this.isDead()) {
            this.lastActivityTime = Date.now();
        }
    }

    isDead() {
        // returns a boolean
    }
}

In my child class, I'm overriding the jump() method like this:

class Child extends Parent {
    jump() {
        if (!super.isDead()) {
            super.jump();
            this.speedY = 30;
        }
    }
}

I have a duplicate if check for isDead(): once in the parent class and once in the child class. How can I refactor the code to avoid repeating this check, while still maintaining functionality and ensuring the parent method is called correctly?

What is the best practice for handling such situations in JavaScript to make the code cleaner and more maintainable?

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Review! To help reviewers give you better answers, we need to know what the code is intended to achieve. Please add sufficient context to your question to describe the purpose of the code. We want to know why much more than how. The more you tell us about what your code is for, the easier it will be for reviewers to help you. Also, edit the title to simply summarise the task, rather than your concerns about the code. \$\endgroup\$ Commented Dec 9 at 19:04

0