Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!