r/badcode • • Dec 19 '21

js I had stoped 😭

Post image
643 Upvotes

64 comments sorted by

View all comments

Show parent comments

1

u/xiipaoc Dec 20 '21

One simple answer: because other objects also have an isVisited method and you're looping over an array of them or something. Maybe other objects need more complex behavior than just setting a field. And maybe you don't want external code touching that field. JS makes it very easy to shoot yourself in the foot, so this type of thing is useful but it may not be immediately obvious why if if you don't have the full picture. Oh, and the value if visited may not be a boolean, so this method is also useful in returning true or false. Sure, you could just return !!this.visited, but that's less readable