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

91
Visualizações
Indirectly accessing iterator returning something empty

If I have a constructed object, and that object has an array property, and I define the Symbol.iterator of the prototype to point to the Symbol.iterator of the internal array, somehow I don't get the same iterator out.

Here's a minimum example:

const thing = function () {
  this.internalArray = [1, 2, 3, 4, 5];
}

Object.defineProperty(thing.prototype, Symbol.iterator, {
  get: function () { return this.internalArray[Symbol.iterator]; }
});

const test = new thing();
const a = test.internalArray[Symbol.iterator]();
const b = test[Symbol.iterator]();
console.log(a); // => Array Iterator {}
console.log(b); // => Array Iterator {}
console.log(a.next()); // => { value: 1, done: false }
console.log(b.next()); // => { value: undefined, done: true }

I cannot fathom why this is happening.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When returning the iterator, you need to bind it to the this.internalArray array instance, else all you have is Array.prototype[Symbol.iterator], which will not iterate over anything that's directly on the test object.

const thing = function () {
  this.internalArray = [1, 2, 3, 4, 5];
}

Object.defineProperty(thing.prototype, Symbol.iterator, {
  get: function () { return this.internalArray[Symbol.iterator].bind(this.internalArray); }
});

const test = new thing();
const b = test[Symbol.iterator]();
console.log(b); // => Array Iterator {}
console.log(b.next()); // => { value: undefined, done: true }

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