Prototypal Inheritance. Douglas Crockford www.crockford.com Five years ago I wrote Classical Inheritance in JavaScript (Chinese Italian Japanese).
It showed that JavaScript is a class-free, prototypal language, and that it has sufficient expressive power to simulate a classical system. My programming style has evolved since then, as any good programmer's should. I have learned to fully embrace prototypalism, and have liberated myself from the confines of the classical model.
My journey was circuitous because JavaScript itself is conflicted about its prototypal nature. New () produces a new object that inherits from .prototype This indirection was intended to make the language seem more familiar to classically trained programmers, but failed to do that, as we can see from the very low opinion Java programmers have of JavaScript. Fortunately, it is easy to create an operator that implements true prototypal inheritance. Function object(o) { function F() {} F.prototype = o; return new F(); } if (typeof Object.create ! JavaScript parasitic inheritance, power constructors and instanceof. Abstract.
This posting shows how one can make Crockford's power constructor functions play nicely with the JavaScript keyword 'instanceof.' [update: March 18, 2008. I asked Crockford what he thinks about this pattern, and he actually discourages the use of 'instanceof' -- instead, he prefers to "... rely instead on good design and polymorphism. "] The inheritance model of JavaScript is based on a combination of the 'new' keyword and the prototype property of (constructor) functions. JavaScript Guru Douglas Crockford (aka 'Yoda') argues that this model (which he calls pseudo-classical) is awkward.
The advantages of power constructor functions include support for private, shared and public variables as well as simplicty (avoiding new and prototype). Function Person(name) { var p = object(OriginalPerson); p.getName = function() {return name;}; return p;} var karl = Person('Karl');var crockford = Guru('Douglas','JavaScript'); Hmm... We will get there in two steps. A fresh look at JavaScript Mixins. (Russian, Japanese) In this article I’ll explore JavaScript mixins in detail, and introduce a less conventional, but to my mind more natural mixin strategy that I hope you’ll find useful.
I’ll finish up with a profiler matrix summarizing the performance impact of each technique. [A big Thank You to the brilliant @kitcambridge for reviewing and improving the code on which this blog is based!] Re-using Functions In JavaScript, every object references a prototype object from which it can inherit properties. Fortunately, when it comes to function re-use, JavaScript offers viable alternatives. The Basics In general computer science, a mixin is a class that defines a set of functions relating to a type (e.g. OK but this is JavaScript, and we have no classes. The Use Case I’m going to discuss a number of mixin techniques but all the coding examples are directed towards one use case: creating circular, oval or rectangular buttons. 1.
In practice, however, such a heavyweight mixin is unnecessary.