Codez Plzīefore we can derive and understand that simpler view of "objects-only" or "object-based", we need to understand what actually gets created and linked when we build up some "inherited" objects in JavaScript. What really matters, at the end of the trick, is that two objects end up linked to each other via the ] chain. but all that is like a magician's sleight of hand that dazzles you over here to distract you from what's really going on over there. Whatever we call it, we normally tap into this object mechanism by following the "OO way": we create a function which we use as a "constructor", and we call that function with new so that we can "instantiate" our "class", which we specify with the constructor function together with its subsequent. So, for our purposes here, let's just say JavaScript is "object-based (OB)" to clarify against "object-oriented (OO)". I'm mildly tempted, however, to just hijack the abbreviation of "OO" to mean "objects-only" instead of "object-oriented", but I bet that probably wouldn't get anywhere, either. Of course, as I argued in part 1, what everybody means when they use some term, matters, so it's far too late to redefine or bend the commonly accepted "object-oriented" to my own purposes, much as I'd like to. Maybe "class-oriented" would have been more accurate, which would have freed us up to use "object-oriented" for JS. JavaScript legitimately is "object-oriented", and perhaps we shouldn't have used that term for the other languages which imply a lot more than just "objects". People often bash JavaScript, but it's one of the few prog languages that let you directly create objects. In contrast to most other languages, JS is somewhat unique that you can actually create objects directly without the notion of classes or other abstractions. In a sense, JS is the purest essence of a "object-oriented (OO)" language, in that it really is all about objects.
In other words, it's only objects, linked to other objects. The key realization, the punchline to this entire article series, is that ] is really only about linking one object to another object, for the purposes of delegating, if the first object cannot handle a property or method access but the second can. I suggested that to understand (and leverage) better the ], we needed to strip away the cruft. Then, in part 2, I addressed several distractions which I think obfuscate JS's true object-oriented identity, including "custom types", "mixins", "polymorphism" (which we'll come back to again later), and even the new "class syntax" coming in ES6.
You dont know js prototypal inheritance series#
In part 1 of this article series (which you should totally go read if you haven't yet!), I revisited an idea not original to me: JS doesn't have "inheritance" in the traditional sense, and what it does have is more appropriately labeled "behavior delegation" - the ability of one object to delegate a method or property access which it cannot handle over to another object which can handle it. I am also honored that David Walsh wanted to publish these articles on his fantastic blog. I'd like to thank the following amazing devs for their generous time in feedback/tech review of this article series: David Bruant, Hugh Wood, Mark Trostler, and Mark McDonnell. Leave the classes to those other languages! Due Thanks When you set aside distractions like mixins, polymorphism, composition, classes, constructors, and instances, and only focus on the objects that link to each other, you gain a powerful tool in behavior delegation that is easier to write, reason about, explain, and code-maintain.
You dont know js prototypal inheritance code#
Instead, embracing that JS has "behavior delegation" (merely delegation links between objects) fits naturally with how JS syntax works, which creates more sensible code without the need of helpers. JavaScript has been plagued since the beginning with misunderstanding and awkwardness around its "prototypal inheritance" system, mostly due to the fact that "inheritance" isn't how JS works at all, and trying to do that only leads to gotchas and confusions that we have to pave over with user-land helper libs.