Reputation: 1110
I don't understand why the Function.prototype.call() can be used this way? As far as I know if a function returns the code after that will not get executed. Did I missed something here?
function Product(name, price) {
this.name = name;
this.price = price;
if (price < 0)
throw RangeError('Cannot create product
"' + name + '" with a negative price');
return this;
}
function Food(name, price) {
Product.call(this, name, price); // if the function returns here why put this.category after this statement?
this.category = 'food'; // will this ever get executed?
}
Food.prototype = Object.create(Product.prototype);
var cheese = new Food('feta', 5);
I understand that both the Product, Food are constructors and we can use call to chain constructors for an object, similar to Java. But why don't put the statement
this.category = 'food';
before
Product.call(this, name, price);
Upvotes: 0
Views: 85
Reputation: 664970
As far as I know if a function returns the code after that will not get executed.
Yes.
Did I missed something here?
return
works local, and ends only the current function call. The Food
function does not return
before setting the .category
property.
Btw, the return
in Product
is unnecessary as constructors don't need to explicitly return.
Upvotes: 1