Sign in
Log inSign up

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

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