Creating custom constructors


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

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) { = 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);


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

"sally's species is " + sally.species


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.


thank you it works!! :slight_smile:


You're very welcome :slight_smile:


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