Social Icons

Pages

Thursday, December 1, 2016

Introduction to Objects I 23/33

More Options
In a constructor, we don't have to define all the properties using parameters. Look at our new Person example on line 1, and see how we set the species to be "Homo Sapiens" (line 4). This means that when we create anyPerson, their species will be "Homo Sapiens". In this way, the values associated with nameand age are not yet assigned, but specieswill always have the same value.
In this case, both sally and holden will have the same species of "Homo Sapiens", which makes sense because that is the same across all people.

Instructions
Create a new object called sally using the Person constructor. Her name is "Sally Bowles" and she is 39. Create another object called holden. His name is "Holden Caulfield" and he is 16.
Edit the sentence printed out such that it includes the age of sally and holdenrespectively.
?
Hint
Create the sally and holden objects with the Person constructor like we have been doing.
You can also make edits to the console.logstatements by using the + operator so you print their ages.
                                                               function Person(name,age,species) {
                                                               this.name = name;
                                                               this.age = age;
                                                               this.species = "Homo Sapiens";
                                                               }

                                                               var sally = new Person("Sally Bowles",39,"Homo Sapiens");
                                                               var holden = new Person("Holden Caulfield",16,"Homo Sapiens");
                                                               console.log("sally's species is " + sally.species + " and she is " + sally.age);
                                                               console.log("holden's species is " + holden.species + " and he is " + holden.age);



No comments:

Post a Comment