//этот код менять нельзя function Cart () { } Cart.prototype.add = function (item) { if (!this.goods) { this.goods = []; } this.goods.push(item); //добавляет товар в корзину }; //редактируемый код function UserCart () { } function Item (idNum, item, sum) { this.idNum = idNum; this.item = item; this.sum = sum; } UserCart.prototype = Object.create(Cart.prototype); const cart = new UserCart(); const item = new Item("1", 'Сhair', "20$"); cart.add(); console.log(cart.goods); 

  • one
    add return item , will return something - splash58
  • one
    and what exactly did you expect to see in the log? - Grundy
  • the goods must be added to the cart using the add function - so what did you want to see in the log? Why do you think that the product has not been added? - Grundy
  • cart.add(item); console.log(cart.goods); - so you will see what worked - splash58
  • eval.in/899791 - splash58

2 answers 2

Add output to the console in the Cart.prototype.add method, like this:

 Cart.prototype.add = function (item) { if (!this.goods) { this.goods = []; } this.goods.push(item); //добавляет товар в корзину console.log('В корзине %s товаров', this.goods.length) }; 

And look at the output

  • Did not quite understand the question, please specify. - Alexey Matveev
  • Eugene, reading the code in this format is very difficult. Please post your code on jsfiddle.net. Give here a link - Alexey Matveev

cart.add (item);

 cart.add(item); 

Complete code:

 //этот код менять нельзя function Cart () { } Cart.prototype.add = function (item) { if (!this.goods) { this.goods = []; } this.goods.push(item); //добавляет товар в корзину }; //редактируемый код function UserCart () { } function Item (idNum, item, sum) { this.idNum = idNum; this.item = item; this.sum = sum; } UserCart.prototype = Object.create(Cart.prototype); const cart = new UserCart(); const item = new Item("1", 'Сhair', "20$"); cart.add(item); console.log(cart.goods);