Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

0

call your function like that:

Number.prototype.percIncrease(1, 10); //"1,10" are examples
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda