Creating custom constructors


#1


Can someone please tell me what is wrong with this code? I get the console.log however i also get an error. See below:


https://www.codecademy.com/en/courses/spencer-sandbox/3/4?curriculum_id=506324b3a7dffd00020bf661#


Oops, try again. Your code threw the following error when sally's name was checked: TypeError: Cannot read property 'name' of undefined


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

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


#2

We need to refer to the instance in global scope, not the this variable.

"sally's species is " + sally.species

#3

There are few errors.

To create a new object of Person class you should use new Person construct.

In the console.log, why you have changed the base code to use this.species? You want to retrieve data from the sally and holden objects (sally.species). And you were supposed to get the age of Sally and Holden from the objects, not just write them.


#4

thank you it works!! :slight_smile:


#5

You're very welcome :slight_smile:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.