0

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.

4
  • 4
    Don't reuse built-in function names for your own functions. It makes for confusing code. Commented Feb 2, 2022 at 17:28
  • @pilchard — No. Because the new keyword is used, this is the same in both those places. Commented Feb 2, 2022 at 17:30
  • 2
    @Quentin - Well, probably, depending on how addAdmin gets called. :-) Commented Feb 2, 2022 at 17:30
  • Thank you for all of the answers. Looks like I was creating a local variable when I didn't intend to. Commented Feb 2, 2022 at 17:46

4 Answers 4

4

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 */
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

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
    };
};

Comments

0

I am assumming Object is a placeholder, because it is a reserved keyword.

What is happening is, your variable var admins = []; is created locally and can noot be accesed with the this. as a result when you set the value in this.admins.push(admin) the admins there is undefined. you should modify your function to read this way

var Obj = function () {
  this.admins = [];

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

const object = new Obj();

object.addAdmin(1);

you should not omit the this keyword like this(no pun intended) if you plan to new the function. Stick to the code above.

    var Obj = function () {
      var admins = [];

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

    const object = new Obj();

    console.log(object)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.