Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Defining a static method on a specific type in Javascript

I have a method that calculate a percentage increase between two values. And in my "class" it looks like this:

percIncrease(a, b) {
    let percent;
    if (b !== 0) {
      if (a !== 0) {
        percent = ((b - a) / a) * 100;
      } else {
        percent = b * 100;
      }
    } else {
      percent = -a * 100;
    }
    return percent.toFixed(3);
  }

now cause I want it reusable from everywhere, I am trying to define it on a type (Number), so I can use it everywhere.

I tried this:

Number.prototype.percIncrease = function (a, b) {
  let percent;
  if (b !== 0) {
    if (a !== 0) {
      percent = ((b - a) / a) * 100;
    } else {
      percent = b * 100;
    }
  } else {
    percent = -a * 100;
  }
  return percent.toFixed(3);
};

but if I try to use it like this :

Number.percIncrease(a,b);

TypeError: Number.percIncrease is not a function

I am not that good in using Javascript terms, but what I am actually trying, is to declare a static method on a specific type (say, Number).

How I would do this?

EDIT:

Or it doesn't have to be static. Working on a value would do the job to. For example this (pseudo code):

myNumber.percIncrease(inCompareToValue: anotherNumber)
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You need a fifferent call of the prototype and maybe change the style a bit.

Number.prototype.percIncrease = function(base) {
    let percent;
    if (base) {
        percent = this
            ? (base - this) * 100 / this
            : base * 100;
    } else {
        percent = -this * 100;
    }
    return percent.toFixed(3);
};


console.log(5..percIncrease(4));

about 4 years ago · Juan Pablo Isaza Report

0

When you define a method in the prototype, it's used when you call the method on an instance. Static methods should be added to the type directly, not the prototype:

Number.percIncrease = function (a, b) {
  let percent;
  if (b !== 0) {
    if (a !== 0) {
      percent = ((b - a) / a) * 100;
    } else {
      percent = b * 100;
    }
  } else {
    percent = -a * 100;
  }
  return percent.toFixed(3);
};

console.log(Number.percIncrease(10, 15));

about 4 years ago · Juan Pablo Isaza Report

0

call your function like that:

Number.prototype.percIncrease(1, 10); //"1,10" are examples
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!