Tengo un objeto de la siguiente manera
var shop = { costPrice: function() { return 100; }, sellingPrice: function() { var calculateProfit = function() { return this.costPrice() * 0.2; } return this.costPrice() + calculateProfit(); } }; console.log(shop.sellingPrice());Pero esto me da el siguiente 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 No estoy seguro de lo que estoy haciendo mal ya que costPrice es una función
calculateProfit debe ser una función de flecha
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());Intenta cambiar estas funciones para que sean flechas:
var shop = { costPrice : function() { return 100; }, sellingPrice : function() { var calculateProfit = () => { return this.costPrice() * 0.2; } return this.costPrice() + calculateProfit(); } }; console.log(shop.sellingPrice());