0

I'm doing some simple javascript learning at the moment and I'm stuck on how to solve this problem. (the basic form comes from Code Academy). The task is to create 3 rabbit objects, each with a different adjective as an attribute. Then, print describeMyself() for each rabbit.

Instead of repeating myself 3 times, I'd like to find a way to solve the problem with a for loop to make it more streamlined/challenging for myself. Here's what I tried:

function Rabbit(adjective) {
    this.adjective = adjective;
    this.describeMyself = function() {
        console.log("I am a " + this.adjective + " rabbit");
    };
}

var rabbit1 = new Rabbit(fluffy);
var rabbit2 = new Rabbit(happy);
var rabbit3 = new Rabbit(sleepy);

for (i=1; i<=3; i++){
    ("rabbit"+i).describeMyself();
}

Obviously, the ("rabbit"+i).describeMyself() is wrong. I want the loop to create "rabbit1", "rabbit2" and "rabbit3". What's the proper syntax here?

4

8 回答 8

3

First of all, the parameters you are passing will result in undefined. If you want to pass strings, then use quotes to mark them as such. Second of all, creating new instances in a for loop means you will have to store them somewhere else, like in an array for instance.

var rabbits = [];
var descriptions = ['fluffy', 'happy', 'white', 'sleepy', 'dreamy'];
for (var i = 0; i < 5; i++) {
    rabbits.push(new Rabbit(descriptions[i]));
}

//Now you have 5 rabbits stored in the rabbits array. Now here's how to make them //egocentric.
for (var i = 0, ii = rabbits.length; i < ii; i++) {
    rabbits[i].describeMyself();
}


var rabbit1 = new Rabbit(fluffy);
var rabbit2 = new Rabbit(happy);
var rabbit3 = new Rabbit(sleepy);

For future reference, don't forget to mark strings with single quotes or double quotes for HTML strings. The above should be:

var rabbit1 = new Rabbit('fluffy');
var rabbit2 = new Rabbit('happy');
var rabbit3 = new Rabbit('sleepy');
于 2012-12-06T10:58:34.300 回答
1

Since the rabbits global variables, they are properties of the window object, so you could use:

for (i=1; i<=3; i++){
    window["rabbit"+i].describeMyself();
}

However,

I'd recommend using the array examples that have been suggested, though, since this is kindof a bad practice. (But nice to know)

于 2012-12-06T11:00:37.877 回答
1

Consider this a hackish answer, but provided you're would do this in global context, you could avoid ussing array and refer to your variables on window obejct like this:

var rabbit1 = new Rabbit('fluffy');
var rabbit2 = new Rabbit('happy');
var rabbit3 = new Rabbit('sleepy');

for (i=1; i<=3; i++){
    window["rabbit"+i].describeMyself();
}

Not to mention even more hackish and evil approach with eval (just putting it out there for reference):

for (i=1; i<=3; i++){
    eval("rabbit"+i+".describeMyself()");
}
于 2012-12-06T11:01:43.850 回答
1

consider using an array of variables then use the index to access them like this

function Rabbit(adjective) {
    this.adjective = adjective;
    this.describeMyself = function() {
        console.log("I am a " + this.adjective + " rabbit");
    };
}

  var rabbit=[];

  rabbit[0]= new Rabbit("fluffy");
  rabbit[1]= new Rabbit("happy");
  rabbit[2]= new Rabbit("sleepy");


for (i=0; i<3; i++){

    rabbit[i].describeMyself();
}
于 2012-12-06T11:01:46.583 回答
0

You can't refer to the variables directly, but you can put them in an array/object:

var rabbits = [];

rabbits[1] = new Rabbit('fluffy');
rabbits[2] = new Rabbit('happy');
rabbits[3] = new Rabbit('sleepy');

for (var i= 0; i < 3; i++){
    rabbits[i + 1].describeMyself();
}
于 2012-12-06T10:57:51.633 回答
0

Sometimes it's useful to get the object to add itself to an array automatically.

​var rabbits = [];
function Rabbit(adjective) {
    this.adjective = adjective;
    this.describeMyself = function() {
        console.log("I am a " + this.adjective + " rabbit");
    };
    rabbits.push(this); // all new Rabbits get added to the array rabbits
}
new Rabbit('happy');
new Rabbit('sleepy');
new Rabbit('fluffy');

for (var i = 0; i < rabbits.length; i++) {
    rabbits[i].describeMyself();
}

​</p>

于 2012-12-06T11:06:34.680 回答
0

You were close, try this:

var attributes = ["fluffy","happy", "sleepy"];

for (i=1; i<=3; i++){
    window["rabbit"+i] = new Rabbit(attributes[i]);
}

for (i=1; i<=3; i++){
    eval(("rabbit"+i)).describeMyself();
}
于 2012-12-06T11:16:52.530 回答
-1

try out this as it works completely fine in order to bring out the perfect result & here is the code:

function Rabbit(adjective)  {     
    this.adjective=adjective;     
    this.describeMyself = function() {         
         console.log("I am a " + this.adjective + " rabbit");
    }; 
}  

var rabbit1 = new Rabbit( "fluffy");
var rabbit2 = new Rabbit("happy"); 
var rabbit3 = new Rabbit("sleepy"); 

rabbit1.describeMyself(); 
rabbit2.describeMyself(); 
rabbit3.describeMyself();   

The problem is every one stucks & forgets to type "this.adjective=adjective;" i.e the 3rd line of the code due to which u will c an error as some undefined objects...Try out the above code to get the perfect output...its correct.

于 2015-08-24T06:45:31.067 回答