My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

call method in javascript !!

Default profile photo
Anonymous
·Oct 15, 2018
// using call to chain constructor 

function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}

var cheese = new Food('feta', 5);
var fun = new Toy('robot', 40);

// code is working fine ,but one thing I am unable to understand that if we omit this from the line Product.call(this, name, price) to Product.call(name, price) then also same output is coming so I was thinking what is need to write this then in above call