r/javascript • • May 28 '19

What are some JavaScript things beginners don't know?

333 Upvotes

383 comments sorted by

View all comments

2

u/[deleted] May 29 '19 edited May 29 '19
  • When to use the Object Oriented model and when to use the Functional Model. (Short summary: If you have a thing that's going to persist, but need to change state, and don't know what actions are going to be done on it until runtime, lean towards object-oriented. If you need to do the same action multiple times to different things, lean towards functional.)

  • Currying. Not that they don't know *how* to curry, but how currying makes things SO much easier once you really get the hang of it -- dependency injection, differentiating between prod and dev databases without having to write a whole bunch of if(isProd){} statements, creating higher order React components, allowing you to use parameters in React's render method...

  • Pointers. Yeah, JS doesn't *use* pointers the way that C, C++, or Rust might use pointers, but under the hood, that's what you're doing when you "pass by reference" vs. "passing by value". Learning a bit of rudimentary C helped me avoid a lot of hard-to-debug "Oh, I'm not copying this, I'm changing it!" bugs.

  • Monads. Eh, don't worry, I'm not going to force you to learn Haskell, but understanding Monads helps you understand what's going on with Promises behind the scenes. One of the big tricky bits of Promises is that Promise.then() always returns a Promise, which is easier to understand if you've fooled around with Identity(val).map(fn).value() and Maybe.of(val)

  • get() and set() - and I'm going to be very controversial here. I think get() and set() should *never, ever be used*, because it can turn a simple assignment of the type

obj.foo = obj2.bar

into a debugging nightmare. If there's a get() on obj2.bar, any arbitrary code could be executed by that assignment. Same if there's a set() on obj.foo.

Now, I know without get() and set() you can't have things like observables, and that it's more difficult to have pub/sub or reactive programming. But is it really that much more difficult to call this.setFoo() or this.getBar() if you need to execute additional code? At least then in the actual code, you give a hint to the programmer that there MAY be something else going on in the assignment - if you know that one of your variable assignments is throwing off a side effect, you have to debugger() until you narrow down the part of the code the side effect is in, and look at each variable assignment (and it still won't be obvious.) By requiring as standard style that anything that executes any code other than a simple get or set be an explicit method call, you can more easily see where errors are introduced. This is one of the reasons I prefer React to Vue (and I love Vue) - Vue's use of observables can make it very easy to get tripped up. (I think Moon.js to actually be a better solution to how Vue works because of it.)

The only time that I can see justifying the use of get() and set() if it's something like

class Distance {
  inMiles = 0
  get inKm(){
    return this.inMiles * 2.2
  }
  set inKm(val){
    this.inMiles = val / 2.2
  }
}
const d1 = new Distance();
d.inMiles = 1;
console.log(d.inKm) // -> 2.2
d.inKm = 22;
console.log(d.inMiles) // -> 10

and even THEN I'd prefer:

class Distance {
  _inMiles = 0
  getMiles(){
    return this._inMiles
  }
  setMiles(val){
    this._inMiles = val;
  }
  getKm(){
    return this._inMiles * 2.2
  }
  setKm(val){
    this._inMiles = val / 2.2
  }
}
const d1 = new Distance();
d.setMiles(1)
console.log(d.getKm()) // -> 2.2
d.setKm(22)
console.log(d.getMiles()) // -> 10