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!

4
  • 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. Commented Apr 13, 2016 at 1:13
  • 1
    Duplicate of stackoverflow.com/questions/1184123/… Commented Apr 13, 2016 at 1:15
  • That's clever, is that the actual way of doing it? Thanks! Commented Apr 13, 2016 at 1: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 ;) Commented Apr 13, 2016 at 1:16

2 Answers 2

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

8 Comments

Thanks, do you know then, how to change a value of all instances at the same time?
@ALSDMinecraft it depends on the approach you take, but I'll post code for each. I would recommend the array approach though haha.
thanks! Actually the array is previously created, I just push one employee when I create one (:
"which interestingly would act exactly like an array" --- not at all, check its .length
@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.
|
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;
}

2 Comments

Thanks, any idea of how to achieve the other question?
Loop over the array, change the value.

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.