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

111
Vistas
Javascript defineProperty strange behavior

So i wrote the following code to add new property and method to the Object prototype, using defineProperty (doing this otherwise would break jQuery):

Object.defineProperty(Object.prototype, "len", {
    value: 0,
    enumerable : false,
    writable: true
});

Object.defineProperty(Object.prototype, "pushById", {
    value: function(object)
    {
        this.len = this.len+1;
        this[object['id']] = object;
        //alert(JSON.stringify(this)); -Displays collection object with its len value equal to 1
    },
    enumerable : false
});

var containerObject = {};
var dataObject = {'id':1, 'name':'Jon Snow'};
containerObject.pushById(dataObject);
console.log(containerObject.len); // -Displays 0 when it should be 1

Why do the properties updating not follow outside the method?

about 4 years ago · Santiago Gelvez
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