Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

113
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda