1

My code looks similarly to this:

I create variable:

var person1;

Than I add this to array:

var people = [];
people.push(person1);

And finally I define what "person1" is:

person1 = new Person("Mike", 25, "London");

The problem is, people that are defined after pushing to array are undefined. For some reasons I can't define all people before putting to array. As I see, "people.push(person1);" actually puts copy of this variable to array. It is possible to do, that I put only reference of variable to the array?

3
  • 1
    since js is pass by reference for objects/arrays only you need to create person1 as an object first, and then edit its properties Commented Feb 10, 2014 at 19:17
  • 2
    You cannot create a reference to a variable in JavaScript. Commented Feb 10, 2014 at 19:18
  • 1
    var people = {}; people.push(person1); does not work, it's not an array ([]). Commented Feb 10, 2014 at 19:20

4 Answers 4

1

You have to create an instance of an object and push the object pointer onto the array. Right now the problem is that you are pushing undefined since you do not instantiate a Person object first. explicitly your code current looks like this:

var person1 = undefined;
var people = [];
people.push(person1);//pushing undefined unto the array
person1 = new Person();//now you have a pointer to an object

You have to first instantiate a Person then both your variable and array entry will point to the same thing

var person1 = new Person(); 
var people = [];
people.push(person1);//pushing a person reference onto the array
person1.someProperty = "lorem" 
people[0].someProperty = "ipsum"
//both person1.someProperty and people[0].someProperty are "ipsum"

Similarly you can instantiate the person directly in the array and create a variable referencing a object in the array.

var people = [new Person()]; //creates an array with a new person object.
people.push(new Person); //adds a new person to the array
var person1 = people[0];
var person2 = people[1];
person1.greet(person2);
Sign up to request clarification or add additional context in comments.

Comments

1

There are no pointers in javascript. person1 is not going to be a pointer.

This variable is going to reference a value. When the variable is used, the current value is referenced. So, when this is used:

var person1;
people = [];
people.push(person1);

undefined shows up because that is what the current value of person1 was. Any future change to person1 will change the value of person1, but it will not affect any of the values in the person array.

It would be more appropriate to take a simpler approach here and just pass in the new value you create

people = [];
people.push(new Person("Mike",25,"London"));

Comments

0

first of all, you need to define people as an array var people = []

then push to array a default personnew person()for example, and then refer to person by index people[1] (for example), and then change that... to what you want

Comments

0

The only thing you can do is:

var person1 = new Person();
var people  = [];
people.push(person1);


people[0].name = "Mike";
people[0].age  = 25;
people[0].city = "London";

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.