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 holden
respectively.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