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

159
Visualizações
where is Buffer.forEach specified?

I know that I can iterate over a Buffer with:

for( const b of buffer ){
  console.log(b);
}

But I noticed I can also call .forEach, .map, etc.

$ node
> const buffer = new Buffer.from([1,2,3]);
undefined
> buffer.forEach( b => console.log( b ) );
1
2
3
undefined
> buffer.map( b => 'value:' + b.toString() ).toString();
'\x00\x00\x00'

Apparently these methods are not specified on the node.js Buffer page, and also I don't understand why it behaves as it does (e.g. '\x00\x00\x00').

Where are the methods .forEach, .map, etc. on the Buffer type specified ?

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

0

Right from the Nodejs Buffer doc.

Buffer instances are also JavaScript Uint8Array and TypedArray instances. All TypedArray methods are available on Buffers.

And, you can see all sorts of methods on a TypedArray including the ones you mention in your question.

If you go look at the implementation of the nodejs Buffer object, you will see that it is made from something they refer to as a FastBuffer. That inherits a bunch of things from Uint8Array which is likely where the methods you asked about come from:

class FastBuffer extends Uint8Array 

And, there is also a function used to populate the prototype with all sorts of additional methods here.


And, for a little diagnostics, if you run this code in nodejs:

let b = Buffer.alloc(100);

console.log(`Buffer instanceof Buffer: ${b instanceof Buffer}`);
console.log(`Buffer instanceof Uint8Array: ${b instanceof Uint8Array}`);

you will see this output:

Buffer instanceof Buffer: true
Buffer instanceof Uint8Array: true

And, then if you walk the prototype chain with this:

let proto = Buffer.prototype;
while (proto) {
    console.log(proto.constructor.name);
    proto = Object.getPrototypeOf(proto);
}

You will get this output that shows the class inheritance:

Buffer
Uint8Array
TypedArray
Object
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