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

101
Vistas
Javascript Arrays as an object field

I am running into a problem with using an array as a Javascript field.

var Object = function () {

  var admins = [];

  this.addAdmin = function(admin){
    this.admins.push(admin)
  }

}

Normally I would expect admin to be pushed into the array admins but instead I get a 'cannot read property 'push' of undefined'.

If I'm not mistaken when I initialized the Object with new Object(), admins = []; should initialize the array. Is this a limitation of Javascript?

Thank you in advance.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

var array creates a local variable. It does not create a property on the object.

You need:

this.admins = [];

or

admins.push(admin) /* without this */
about 4 years ago · Juan Pablo Isaza Denunciar

0

In your function admins is a local variable to the function. You need to declare admins as a property on the instance.

function Obj(){
    this.admins = [];
}
Obj.prototype.addAdmin = function(admin){
    this.admins.push(admin);
}

obj = new Obj();
obj.addAdmin('tester');

Also, because Object is the global base object, don't create functions or objects named Object.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I suspect you've gotten confused (which is easy :-) ) because you've seen code like this:

class Obj {
    admins = [];

    addAdmin(admin) {
        this.admins.push(admin);
    }
}

That uses the modern class and class fields syntax to puts an admins property on the object constructed via new Obj. (Note there's no var before admins = [];.) But in your code, you've used the older function-based syntax. Within your function, var admins = []; just creates a local variable, not a property.

I'd suggest that if you want to create constructor functions, using the new class syntax above is the simpler, more powerful way to do that. If you want to use the older syntax, though, other answers have shown how, but for completeness either make admins a property of the object:

let Obj = function() {
    this.admins = []; // ***
  
    this.addAdmin = function(admin){
        this.admins.push(admin)
    };
};

or perhaps with addAdmin on the prototype:

let Obj = function() {
  this.admins = []; // ***
};
Obj.prototype.addAdmin = function(admin){
    this.admins.push(admin)
};

or use the fact addAdmins closes over the call to Obj, and thus the local admins:

let Obj = function() {
    const admins = [];
  
    this.addAdmin = function(admin){
        admins.push(admin) // <=== No `this.` here, you want to close over the
                           // `admins` local
    };
};
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