Transcript from the "OLOO Pattern" Lesson
[00:00:21] So I want us to simplify. I have a different style of approaching the way we use objects, and the prototype language between them and I call this style of coding OLOO, Objects Linked to Other Objects. I needed some kind of catchy term to contrast with OO and the sad reality is that object oriented is actually the best term.
[00:01:33] And that is trademarked, so you need to pay me lots of money if you want to use it. I'm kidding. So let's look at how OLOO approaches things. Remember the class syntax. This is what we talked about a little bit before. Classes are a way of patterning some relationship between different entities and instantiating them.
[00:01:53] But under the covers, remember, that that class is actually implying all of this gunk right? It's actually implying all of these prototypes. So what if we could do a style of coding that accomplishes the same thing but without any of the baggage of thinking or designing with classes, needing to worry about prototypes, or super, or any of that other stuff, and certainly never needing to use the new key word again.
[00:02:18] What if we could just make objects link to other objects? Here's the OLOO style representation of that code. What I want you to see here is that I have only objects now. Workshop is not a function. It's not a class. It's just an object. AnotherWorkshop is just an object.
[00:02:43] I am linking it via Object.create the workshop but it is just an object. And the JSRecentParts is just an object that is linked to another workshop. So when I call JSRecentPart.setteacher, it doesn't have that method but it goes to another workshop who doesn't have that method, but it goes to this workshop.
[00:03:06] And then it finds set teacher, and it uses this binding of the call site to set a property on JSRecentPart's. So we're getting the same benefits of a class system. But notice what is missing now. We don't have any more .prototypes, we don't have any more constructor functions, and we don't have any more new keywords.
>> Kyle Simpson: Not only do I argue that this is a simpler style and if you looked line by line comparison, there is essentially very little syntactic difference between this and the class equivalent. It's not that class equivalent is just massively more sugary or something. They're essentially very close in equivalence.
[00:04:59] But where this one is exposing to you directly instead of pretending with an artifice that it's something that it's not, it's exposing to you directly, these are just objects linked to other objects.
>> Kyle Simpson: Now, how does object.create do that magic? Well this is an old polyfill for object.create.
[00:05:21] We don't need polyfills mostly for pre-ES5 anymore. But this was the pre-ES5 polyfill for object.create. And look at what it does, is it literally makes an empty function. Cuz we don't even care what the function is. It sets its prototype to sum O, and it calls new on that who cares whatever function to give us the newly created object.
[00:05:46] So, in other words, the way object.create does the magic of OLOO, is we take all of those constructor functions that nobody cares about and all those .prototypes that nobody wants to write, and all those new keywords that are just confusing. And we hide them inside of object.create, okay, leaving in our code just this clean, simple linkage between objects.