I'm new to JS, still struggling in understanding 'this' in Chaining Constructor Functions. Below is an example:
let Product = function(name, price) {
this.name = name;
this.price = price;
}
let TaxedProduct = function(name, price, taxRate) {
Product.call(this, name, price);
this.taxRate = taxRate;
}
let hat = new TaxedProduct("Hat", 100, 1.2);
I don't understand why we should use Product.call(this, name, price);
, why can't we just use Product(name, price);
? I understand 'this' is used for binding, but in the construction of TaxedProduct
, this
is already referred to newly created TaxedProduct
object, so use Product(name, price);
is pretty much like copy and paste the Product
construction content into TaxedProduct
's as:
let TaxedProduct = function(name, price, taxRate) {
//Product(name, price); just like copy and paster below two statements
this.name = name;
this.price = price;
this.taxRate = taxRate;
}