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

106
Visualizações
What is the technical reasons behind omitting new Object methods from object prototye in javascript?

Object constructor function got several methods during the past updates of js like apply, assign, entries, fromEntries, keys, values...

These would be excellent candidates to be included in object prototype.

Object.prototype.values = function(f) {
    return Object.values(this)
}

We could even combine them to implement map or filter:

Object.prototype.map = function(f) {
    return Object.fromEntries(Object.entries(this).map(f))
}

// now we could do...
obj1 = {a:1, b:2}
obj1.values() // [1,2]
obj1.map([a,b] => ['x'+a: b+1]) // {xa:2, xb:3}

This syntax would have been unquestionably superior compared to eg.

Object.fromEntries(Object.entries(obj1).map([a,b] => ['x'+a: b+1])))

But even

obj1.entries()
    .map([a,b]=>['x'+a: b+1])
    .fromEntries()

is much more readable.

Backward compatibility doesn't seem to be a problem, since further objects in the prototype chain would mask the methods (eg. Array.prototype.map would still work properly).

Other route was taken, and there must be a technical reason. I'm pretty much curious what it is.

Are there any examples (possibly in legacy code) where the above approach would fail?

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

0

Having too many extra properties on all objects is problematic because it would be difficult or confusing to use those names as regular keys on objects.

For instance, values is a valid key name on an object, e.g. {values: [1,2,3]}. In the example in the question, if this property could be modified, then calling .values() on an object would not always work. If this property is non-configurable, then it would be impossible to use values as a regular key name, which is undesirable.

about 4 years ago · Juan Pablo Isaza Relatório

0

Adding to what Unmititgated already stated, adding these methods to the prototype would make them unavailable on objects created from null, which shouldn't be the case on newly introduced object methods.

Object.prototype.values = function() {
    return Object.values(this)
}

let obj = Object.create(null);
obj.name = "Peter";
obj.age = 34;

console.log(Object.values(obj)); // works

console.log(obj.values()); // error

The proposal for Object.hasOwn explicitly mentions this aspect.

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