Open douglascalhoun opened 9 years ago
I believe that cart.items = items; in createCart masks cartProto.items each time.
Correct.
I believe the new cart's items property will be a new empty object with prototypally inherits from the array ["apple", "pear", "orange"] rather than a copy, which could be obtain using cart.items = items.slice();.
Yes, items.slice()
would have made a better example. I did this out of habit to share memory by referencing the delegate for unchanged properties, but it's not needed in this context. Using Object.create()
to make array "copies" should be considered an anti-pattern. Thank you for your comments.
ch02#side-effects
I’m not sure that this is true. I think it is a shared reference to the array
["apple", "pear", "orange"]
.cartProto.items
should remain an empty array.I believe that
cart.items = items;
increateCart
maskscartProto.items
each time.Also, it is stated:
cart.items = Object.create(items);
I believe the new cart's items property will be a new empty object with prototypally inherits from the array
["apple", "pear", "orange"]
rather than a copy, which could be obtain usingcart.items = items.slice();
.