9

I have a constructor object in my code which is:

function Employee(){
    this.name = names[Math.floor(Math.random() * names.length)];
    this.age=1;
    this.level=1;
    this.production=400;
    this.totalprod=0;
}

So when I create a new Employee I just say:

var employee1 = new Employee();

So then I can manipulate this instance of the object. Now, I want this objects to be created dynamically with the variable names: employee1, employee2, employee3 and so on.. Is there a way to achieve this or is it impossible?

And the other question that I have, say that I want to change the age of all instances at the same time, is there a way to do this? Thanks in advance and sorry if the question in silly, I'm learning!

EDIT: This is not the same as the other question as I'm using a constructor Object, not a literal Object, and, apart from that, I ask another question which is how to change a property of all instances at the same time, thanks!

Khaled Awad
  • 1,644
  • 11
  • 24
ALSD Minecraft
  • 1,421
  • 3
  • 12
  • 18
  • You can't do exactly what you're asking, but you could achieve a similar result by making an array and pushing each new employee into it. – m0meni Apr 13 '16 at 01:13
  • 1
    Duplicate of http://stackoverflow.com/questions/1184123/is-it-possible-to-add-dynamically-named-properties-to-javascript-object – Akshat Mahajan Apr 13 '16 at 01:15
  • That's clever, is that the actual way of doing it? Thanks! – ALSD Minecraft Apr 13 '16 at 01:15
  • @AkshatMahajan not exactly the same as I'm working with object constructor, not literal and mine has another question at the end, related to the original ;) – ALSD Minecraft Apr 13 '16 at 01:16

2 Answers2

6

This isn't really possible without the use of something like eval, which is bad practice.

Instead if you knew how many employees you wanted to make in advance you could do something like this.

Example: https://jsfiddle.net/dbyw7p9x/

function makeEmployees(n) {
  var employees = new Array(n)
  for (var i = 0; i < n; ++i) {
    employees[i] = new Employee()
  }
  return employees
}

alternatively you could make also make it return an object which interestingly, while not exactly the same as the array, would be accessed in the same way as an array using numbers inside square brackets obj[0], obj[1], obj[2], obj[3] etc.

function makeEmployeesObj(n) {
  var employees = {}
  for (var i = 0; i < n; ++i) {
    employees[i] = new Employee()
  }
  return employees
}

To change a property for each approach you can do:

// Array
for (var i = 0; i < e1.length; ++i) {
    e1[i].age = 2
}

// Object
Object.keys(e2).forEach(function(key) {
    e2[key].age = 2
})
m0meni
  • 16,006
  • 16
  • 82
  • 141
  • Thanks, do you know then, how to change a value of all instances at the same time? – ALSD Minecraft Apr 13 '16 at 01:20
  • @ALSDMinecraft it depends on the approach you take, but I'll post code for each. I would recommend the array approach though haha. – m0meni Apr 13 '16 at 01:22
  • thanks! Actually the array is previously created, I just push one employee when I create one (: – ALSD Minecraft Apr 13 '16 at 01:23
  • @ALSDMinecraft anything else you want to know? – m0meni Apr 13 '16 at 01:26
  • 1
    "which interestingly would act exactly like an array" --- not at all, check its `.length` – zerkms Apr 13 '16 at 01:30
  • @zerkms perhaps I should I expressed more clearly that I meant that even though it's an object when you access its members it'll look exactly the same as when you access the members of an array e.g. `obj[0], obj[1], obj[1]` – m0meni Apr 13 '16 at 01:31
  • 1
    @AR7 it's like that for exactly the opposite reason: it's arrays behave like object, because they **are** objects, not the other way around. – zerkms Apr 13 '16 at 01:32
  • @zerkms I'll try to use more care when commenting next time. I'm doing it a little mindlessly. I understand that arrays are objects, and I apologize for failing to make that clear. All I'm saying is arrays are typically accessed using square brackets with numbers inside, and that this object would similarly be accessed using square brackets with numbers inside. That's all I was trying to convey. – m0meni Apr 13 '16 at 01:34
4

Here is one way to do it, using an array, we push to new Employees to it, and return that array:

To add a specific value, in this case age, I recommend passing it in as a parameter to your Employee constructor, you can do this with all of the this parameters if you like:

Notice in the JsBin that all the ages are different, they are actually the value of n:

Working example: JSBin

    function Employee(age){
    this.name = 'something';
    this.age= age;
    this.level=1;
    this.production=400;
    this.totalprod=0;
}


function maker(n) {
  var arr = [];
  while (n > 0) {
    arr.push(new Employee(n));
    n--;
  }
  return arr;
}
omarjmh
  • 13,632
  • 6
  • 34
  • 42