Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

0

call your function like that:

Number.prototype.percIncrease(1, 10); //"1,10" are examples
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda