3

I have a really simple JavaScript question...

I have a function, Pets, that contains three variables: an integer, a string, and an array defined as follows:

function Pets() {
    var index;
    var name;
    var animals = ["cat", "dog"];
}

In another function just below this one I declare a variable of type Pets and push "mouse" onto the animals array. I then do a console log to see the values.

function Test() {
    ...
    var pets = new Pets();
    pets.index = 1;
    pets.name = "My Pets";
    pets.animals.push("mouse");
    console.log(pets);
}

For whatever reason I didn't see any output from the console.

I then added some more console logging before and after calling animals.push:

    var pets = new Pets();
    pets.index = 1;
    pets.name = "My Pets";
    console.log("BEFORE PUSH");
    console.log(pets);
    pets.animals.push("mouse");
    console.log("AFTER PUSH");

And here is my output:

BEFORE PUSH
Pets {index: 1, name: "My Pets"} 

Note that I only see the first console log and not the second (after the push) which I presume indicates a problem with calling push on animals.

Also note that the console output shows that Pets has a property called index and a property called name but nowhere does it indicate that there is also a property called animals which is my array.

I tried changing the array declaration to:

var animals = new Array();

but that didn't seem to help either.

Is there something special that I have to do to initialize the animals array before I can push elements on to it? More generally, why doesn't the property animals even show up as part of the Pets class?

Thank you,

Jan

Jan Tacci
  • 3,131
  • 16
  • 63
  • 83
  • I figured it was asked somewhere else. A quick search did not yield an answer, however. I suppose my search terms were not correct. Delete away! – Jan Tacci Jul 25 '14 at 01:51
  • Does duplicating a question on Stack Overflow lower my reputation? :O – Jan Tacci Jul 25 '14 at 01:53
  • No, it just marks your question as duplicate and a banner appears above the question redirecting users to the original question. Nothing changes with your [reputation](http://stackoverflow.com/users/1586383/jan-tacci?tab=reputation) – Patrick Evans Jul 25 '14 at 01:58
  • Okay... well I accepted your answer so let's hope it sticks. :) – Jan Tacci Jul 25 '14 at 02:02

3 Answers3

3

You are declaring your variables in a way that will not make them visible outside the the Pets constructor function

This is why you do not see the second console because the line

pets.animals.push("mouse");

will throw the error TypeError: Cannot read property 'push' of undefined (or similar depending on browser). This will cause the rest of the script to stop executing.

The only reason you see index and name in the first console log is because you are creating them on the object after creating your Pets object. You need declare them using this operator or by setting them up on the prototype

function Pets() {
    this.index = null;
    this.name = null;
    this.animals = ["cat", "dog"];
}
Patrick Evans
  • 41,991
  • 6
  • 74
  • 87
0

Have you tried assigning the variable to the window during declaration instead of using var? Set it like window.myVar = ["var1", "var2"]

Phil Young
  • 1,334
  • 3
  • 21
  • 43
0

You can't access any of the properties of Pets this way, because they are not exposed to outer functions. You are creating the properties index and name by saying

pets.index = ...

You have to change your code like so:

function Pets() {
    this.index;
    this.name;
    this.animals = ["cat", "dog"];
}
function Test() {
    var pets = new Pets();
    pets.index = 1;
    pets.name = "My Pets";
    pets.animals.push("mouse");
    console.log(pets);
}
Test();
jhinzmann
  • 968
  • 6
  • 15