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

142
Visualizações
TypeError defining two Array.prototype

I am defining two prototypes for array and one prototype needs to call other like that:

Array.prototype.swap = function (x, y) {
      var b = this[x];
      this[x] = this[y];
      this[y] = b;
      return this;
    };
Array.prototype.rotate = function () {
      for (idx in this) {
        nextIdx = idx + 1 < this.length ? idx + 1 : 0;
        this.swap(idx, nextIdx);
      }
      return this;
    };

When I run [1,2,3,4].rotate() i get an error

> TypeError: this.swap is not a function
>     at Array.rotate (/home/user/array.js:11:10)

I think it's impossible to use an user-defined prototype inside another, but I want to understand why, and if I'm wrong (it's possible), how can I use? Thanks!

about 4 years ago · Juan Pablo Isaza
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