BenRongey / BenApprenticeship

0 stars 0 forks source link

You Don't Know JS - this & Object Prototypes #23

Open BenRongey opened 5 years ago

BenRongey commented 5 years ago

Read: https://github.com/getify/You-Dont-Know-JS/blob/master/this%20&%20object%20prototypes/README.md

Questions:

function addFive(i) {
  return i + 5;
}

console.log(addFive(10));

Note: Kyle Simpson pushes the OLOO pattern over OOP hard in this book, but we're in that delusional group that speaks OOP in JS. We make heavy use of ES6 classes. It is absolutely useful to know what JS is actually doing and not pretend like JS classes are like classes in other languages. However, we think the language and syntax of classes and OOP is a very useful abstraction for discussing design problems and expressing things in code.

Basically all frameworks and existing client code you will encounter are expressed in OOP-like ways. React for example, draws a distinction between component classes and elements (instances). You won't see much OLOO code out there-- and won't see us make heavy use of that pattern.

The examples Kyle gives of the problems with ES6 "classes" are useful for illustrating that nothing has really changed-- JS is still doing the same thing under the hood-- but he's got to contort pretty hard to expose the issues with the syntax. You just won't see much code like that in the wild (e.g. mixing prototype style with ES6 classes, or dynamically re-writing class definitions all over the place). And yeah, it is useful to pretend class syntax makes static classes definitions-- it is a useful signal to yourself and other programmers not to monkey with that particular object!