1

How would I call the static function calculator()?. I know a static function does not need an instance of the class to be called. However, trying the following wont work:

class TaxCalculator {
  static calculator(total) {
    return total * .05;
  }
}

new TaxCalculator().calculator(50);

0

2 Answers 2

1

You need to call the function in a static context, i.e. directly on the class itself, and not on an instance of the class:

class TaxCalculator {
  static calculator(total) {
    return total * .05;
  }
}


console.log(TaxCalculator.calculator(50));

Sign up to request clarification or add additional context in comments.

4 Comments

I understand what you are saying at it works but in the quiz i was doing i was given the following options. addTax(50) new TaxCalculator().calculator($50), new TaxCalculator().calculator(50);
also calculator(50)
None of those would work. You're sure it's not a Java question, right? Instead of JavaScript?
Its from the Linkedin Skills test. Its the only question I got wrong
0

You can access static method directly with Class name.

class TaxCalculator {
  static calculator(total) {
    return total * .05;
  }
}

  console.log(TaxCalculator.calculator(10000));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.