Sign in
Log inSign up

Prototype confusion in Javascript !!

Default profile photo
Anonymous
·Aug 11, 2018
function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

// Greeting
Person.prototype.greeting = function(){
  return `Hello there ${this.firstName} ${this.lastName}`;
}

// Customer constructor
function Customer(firstName, lastName, phone, membership) {
  Person.call(this, firstName, lastName);

  this.phone = phone;
  this.membership = membership;
}

// Inherit the Person prototype methods
Customer.prototype = Person;

let detail1 = new Customer("hom", "kom", 989, "l");
detail1.greeting()//  it is producing error that greeting is not a function

Why when I am running detail1.greeting() is producing error even though I have set the prototype.

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct