Skip to content

Mistaken example? #45

@nmaxcom

Description

@nmaxcom

Hi. At the end of the 2nd chapter you provide the following piece of code saying:

When the items variable is changed to reference another list of items, we’re in for a world of hurt—the todo API still works with the value items used to have, but items is referencing something else now.

var items = ['a', 'b', 'c']
var todo = checklist(items)
todo.check()
console.log(items)
// <- ['b', 'c']
items = ['d', 'e']
todo.check()
console.log(items)
// <- ['d', 'e'], would be ['c'] if items had been constant
function checklist(items) {
  return {
    check: () => items.shift()
  }
}

IMHO items and the checklist function are just forming a closure. So once that todo var is created and items is referenced to another thing, it doesn't matter what is done to items, it's irrelevant to the code.

I'm just a hobbyist so I thought someone should take a look at it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions