d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Script > Begginer Question
Add Reply New Topic New Poll
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Sep 23 2013 08:14am
I dont quite understand the syntax


instructions
Code
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 any Person, their species will be "Homo Sapiens". In this way, the values associated with name and age are not yet assigned, but species will 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.



my code
Code
function Person(name,age) {
this.name = name;
this.age = age;
this.species = "Homo Sapiens";
}

var sally = new Object(Person){
sally.name = "Sally Bowles";
sally.age = 39;
sally.species = "";
}
var holden = new Object(Person){
holden.name = "Holden Caulfield";
holden.age = 16;
holden.species="";
}
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);
Member
Posts: 827
Joined: Jan 16 2012
Gold: 0.00
Warn: 10%
Sep 23 2013 11:16am
So... What is it that you dont understand?
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Sep 23 2013 11:22am
it kept giving me a syntax error, but i resolved it myself, i was trying to origanally do it like up above..

then i switched it to

var sally = new person("Sally Bowles",39,"Homo Sapiens");
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll