0

So I have an object constructor, but when I try and put an object in, the array cmndlist always has x amount of the last object defined. (Basically they all end up the same)

function command(name, category, help, callback) {
    this.name = name;
    this.help = help;
    this.use = callback;

    cmndlist[category].push(this);
}; 

Here's the code where I define the objects too.

command("foo",0,"foo", function(message){
    //code
});

command("bar",1,"bar", function (message){
    //code
}); 

command("foobar",1,"foobar", function (message){
    //code
}); 

So in this example, all the objects in the array will have the same properties as foobar.

I don't want that, I want it to have all three of the objects.

Kikasuru
  • 11
  • 2

1 Answers1

1

To get a new object we need to add the 'new' before our call to the command. Otherwise we are referencing the same object instance each time. Take a look here for a description of object construction in JavaScript. https://www.w3schools.com/js/js_object_constructors.asp

var cmndlist = [];

function command(name, category, help, callback) {
    this.name = name;
    this.help = help;
    this.use = callback;

    if(cmndlist.hasOwnProperty(category) == false) cmndlist[category] = [];

    cmndlist[category].push(this);
}; 


new command("foo",0,"foo", function(message){
    //code
});

new command("bar",1,"bar", function (message){
    //code
}); 

new command("foobar",1,"foobar", function (message){
    //code
}); 
Bibberty
  • 4,670
  • 2
  • 8
  • 23
  • 1
    Please explain in the answer what you changed and why it was necessary. – Bergi Dec 17 '18 at 22:10
  • We need to a 'new' call to create a new object instance, otherwise our call is simply referencing a single object. (Notice the call to command now has new in front). Object are passed by reference (essentially an address pointer to the actual object) So when we add to the array without a call to new, we are just adding a new pointer to the same object. – Bibberty Dec 17 '18 at 22:15
  • Per @Bergi I would consider a syntax more like let newCommand = new Command(x,y,z); and then append. Will be easy to read and maintain later. – Bibberty Dec 17 '18 at 22:19
  • @PaulThomas "*otherwise our call is simply referencing a single object*" - *which* object is that? Adding that to the explanation would greatly help – Bergi Dec 17 '18 at 22:21
  • @Bergi added a link to object construction. Kikasuru note in the link it recommends object constructors be named with uppercase. function Command(x,y,z) – Bibberty Dec 17 '18 at 22:27
  • Uh, [w3schools is heavily despised](https://meta.stackoverflow.com/questions/280478/why-not-w3schools-com)... – Bergi Dec 17 '18 at 22:30
  • @Bergi I have no affinity. Try this instead. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor – Bibberty Dec 17 '18 at 22:32
  • 1
    Or another useful one. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new – 3limin4t0r Dec 17 '18 at 22:34
  • @JohanWentholt snap :) – Bibberty Dec 17 '18 at 22:34