Skip to content

Questions re: Chapter 2: Minimize Side Effects #2

@douglascalhoun

Description

@douglascalhoun

ch02#side-effects

createCart = function (items) {
  var cart = Object.create(cartProto); 
  cart.items = items;
  return cart;
}

"At this point, cart.items is a reference to the prototype items attribute.”

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; in createCart masks cartProto.items each time.

Also, it is stated:

cart.items = Object.create(items);

Now the new cart will have its own copy of the item data, so changes will not be de‐
structive to storedCart.

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();.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions