Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: Person.greet is not a function

function Person(name, age, greet) { 
  this.name = 'Josh';
  this.age = 15;
  this.greet = function(greeting) {
   console.log('Hello I am ', this.name)
  }
}

Person.greet();

I’m trying to get the console to show "Hello I am Josh", I get the same error saying Person.greet is not a function

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This seems to be a constructor function, and not an object, hence, you need to call new to create instance of the constructor function before invoking the methods.

// on a side note, if you're not going to use the arguments, better remove them altogether
function Person(name, age, greet) { 
  this.name = 'Josh';
  this.age = 15;
  this.greet = function(greeting) {
   console.log('Hello I am ', this.name)
  }
}

const person = new Person();
person.greet();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading