I have an object as follows
var shop = {
costPrice: function() {
return 100;
},
sellingPrice: function() {
var calculateProfit = function() {
return this.costPrice() * 0.2;
}
return this.costPrice() + calculateProfit();
}
};
console.log(shop.sellingPrice());
But this gives me the following error
objects.html:16 Uncaught TypeError: this.costPrice is not a function
at calculateProfit (objects.html:16)
at Object.sellingPrice (objects.html:19)
at objects.html:22
Not sure what I am doing wrong as costPrice is a function
calculateProfit must be an arrow function
var shop = {
costPrice : function() {
return 100;
},
sellingPrice : function() {
var calculateProfit = () => {
return this.costPrice() * 0.2;
}
return this.costPrice() + calculateProfit();
}
};
console.log(shop.sellingPrice());
var shop = {
costPrice: function() {
return 100;
},
sellingPrice: function() {
var calculateProfit = () => this.costPrice() * 0.2;
return this.costPrice() + calculateProfit();
}
};
console.log(shop.sellingPrice());
Try changing these functions to be arrows:
var shop = {
costPrice : function() {
return 100;
},
sellingPrice : function() {
var calculateProfit = () => {
return this.costPrice() * 0.2;
}
return this.costPrice() + calculateProfit();
}
};
console.log(shop.sellingPrice());