r/javascript • • Jun 18 '17

Pass by reference !== pass by value

3.3k Upvotes

272 comments sorted by

View all comments

Show parent comments

12

u/dvlsg Jun 18 '17 edited Jun 18 '17

I'm mostly surprised this post got so many upvotes. It's an awful example.

It doesn't even make sense in the scope of javascript. If you pass the cup to a function, and then the function does something to the insides (properties) of the cup (fill it with coffee), the original cup is absolutely going to be modified.

class Cup {
  constructor() {
    this.coffee = 0;
  }
}

function fillCup(cup) {
  cup.coffee = 100;
}

const cup = new Cup();
fillCup(cup);
console.log(cup.coffee); // 100