Classes vs. Objects

We’ve just seen various theoretical explorations and mental models of “classes” vs. “behavior delegation”. But, let’s now look at more concrete code scenarios to show how’d you actually use these ideas.

We’ll first examine a typical scenario in front-end web dev: creating UI widgets (buttons, drop-downs, etc).

Widget “Classes”

Because you’re probably still so used to the OO design pattern, you’ll likely immediately think of this problem domain in terms of a parent class (perhaps called Widget) with all the common base widget behavior, and then child derived classes for specific widget types (like Button).

Note: We’re going to use jQuery here for DOM and CSS manipulation, only because it’s a detail we don’t really care about for the purposes of our current discussion. None of this code cares which JS framework (jQuery, Dojo, YUI, etc), if any, you might solve such mundane tasks with.

Let’s examine how we’d implement the “class” design in classic-style pure JS without any “class” helper library or syntax:

  1. // Parent class
  2. function Widget(width,height) {
  3. this.width = width || 50;
  4. this.height = height || 50;
  5. this.$elem = null;
  6. }
  7. Widget.prototype.render = function($where){
  8. if (this.$elem) {
  9. this.$elem.css( {
  10. width: this.width + "px",
  11. height: this.height + "px"
  12. } ).appendTo( $where );
  13. }
  14. };
  15. // Child class
  16. function Button(width,height,label) {
  17. // "super" constructor call
  18. Widget.call( this, width, height );
  19. this.label = label || "Default";
  20. this.$elem = $( "<button>" ).text( this.label );
  21. }
  22. // make `Button` "inherit" from `Widget`
  23. Button.prototype = Object.create( Widget.prototype );
  24. // override base "inherited" `render(..)`
  25. Button.prototype.render = function($where) {
  26. // "super" call
  27. Widget.prototype.render.call( this, $where );
  28. this.$elem.click( this.onClick.bind( this ) );
  29. };
  30. Button.prototype.onClick = function(evt) {
  31. console.log( "Button '" + this.label + "' clicked!" );
  32. };
  33. $( document ).ready( function(){
  34. var $body = $( document.body );
  35. var btn1 = new Button( 125, 30, "Hello" );
  36. var btn2 = new Button( 150, 40, "World" );
  37. btn1.render( $body );
  38. btn2.render( $body );
  39. } );

OO design patterns tell us to declare a base render(..) in the parent class, then override it in our child class, but not to replace it per se, rather to augment the base functionality with button-specific behavior.

Notice the ugliness of explicit pseudo-polymorphism (see Chapter 4) with Widget.call and Widget.prototype.render.call references for faking “super” calls from the child “class” methods back up to the “parent” class base methods. Yuck.

ES6 class sugar

We cover ES6 class syntax sugar in detail in Appendix A, but let’s briefly demonstrate how we’d implement the same code using class:

  1. class Widget {
  2. constructor(width,height) {
  3. this.width = width || 50;
  4. this.height = height || 50;
  5. this.$elem = null;
  6. }
  7. render($where){
  8. if (this.$elem) {
  9. this.$elem.css( {
  10. width: this.width + "px",
  11. height: this.height + "px"
  12. } ).appendTo( $where );
  13. }
  14. }
  15. }
  16. class Button extends Widget {
  17. constructor(width,height,label) {
  18. super( width, height );
  19. this.label = label || "Default";
  20. this.$elem = $( "<button>" ).text( this.label );
  21. }
  22. render($where) {
  23. super.render( $where );
  24. this.$elem.click( this.onClick.bind( this ) );
  25. }
  26. onClick(evt) {
  27. console.log( "Button '" + this.label + "' clicked!" );
  28. }
  29. }
  30. $( document ).ready( function(){
  31. var $body = $( document.body );
  32. var btn1 = new Button( 125, 30, "Hello" );
  33. var btn2 = new Button( 150, 40, "World" );
  34. btn1.render( $body );
  35. btn2.render( $body );
  36. } );

Undoubtedly, a number of the syntax uglies of the previous classical approach have been smoothed over with ES6’s class. The presence of a super(..) in particular seems quite nice (though when you dig into it, it’s not all roses!).

Despite syntactic improvements, these are not real classes, as they still operate on top of the [[Prototype]] mechanism. They suffer from all the same mental-model mismatches we explored in Chapters 4, 5 and thus far in this chapter. Appendix A will expound on the ES6 class syntax and its implications in detail. We’ll see why solving syntax hiccups doesn’t substantially solve our class confusions in JS, though it makes a valiant effort masquerading as a solution!

Whether you use the classic prototypal syntax or the new ES6 sugar, you’ve still made a choice to model the problem domain (UI widgets) with “classes”. And as the previous few chapters try to demonstrate, this choice in JavaScript is opting you into extra headaches and mental tax.

Delegating Widget Objects

Here’s our simpler Widget / Button example, using OLOO style delegation:

  1. var Widget = {
  2. init: function(width,height){
  3. this.width = width || 50;
  4. this.height = height || 50;
  5. this.$elem = null;
  6. },
  7. insert: function($where){
  8. if (this.$elem) {
  9. this.$elem.css( {
  10. width: this.width + "px",
  11. height: this.height + "px"
  12. } ).appendTo( $where );
  13. }
  14. }
  15. };
  16. var Button = Object.create( Widget );
  17. Button.setup = function(width,height,label){
  18. // delegated call
  19. this.init( width, height );
  20. this.label = label || "Default";
  21. this.$elem = $( "<button>" ).text( this.label );
  22. };
  23. Button.build = function($where) {
  24. // delegated call
  25. this.insert( $where );
  26. this.$elem.click( this.onClick.bind( this ) );
  27. };
  28. Button.onClick = function(evt) {
  29. console.log( "Button '" + this.label + "' clicked!" );
  30. };
  31. $( document ).ready( function(){
  32. var $body = $( document.body );
  33. var btn1 = Object.create( Button );
  34. btn1.setup( 125, 30, "Hello" );
  35. var btn2 = Object.create( Button );
  36. btn2.setup( 150, 40, "World" );
  37. btn1.build( $body );
  38. btn2.build( $body );
  39. } );

With this OLOO-style approach, we don’t think of Widget as a parent and Button as a child. Rather, Widget is just an object and is sort of a utility collection that any specific type of widget might want to delegate to, and Button is also just a stand-alone object (with a delegation link to Widget, of course!).

From a design pattern perspective, we didn’t share the same method name render(..) in both objects, the way classes suggest, but instead we chose different names (insert(..) and build(..)) that were more descriptive of what task each does specifically. The initialization methods are called init(..) and setup(..), respectively, for the same reasons.

Not only does this delegation design pattern suggest different and more descriptive names (rather than shared and more generic names), but doing so with OLOO happens to avoid the ugliness of the explicit pseudo-polymorphic calls (Widget.call and Widget.prototype.render.call), as you can see by the simple, relative, delegated calls to this.init(..) and this.insert(..).

Syntactically, we also don’t have any constructors, .prototype or new present, as they are, in fact, just unnecessary cruft.

Now, if you’re paying close attention, you may notice that what was previously just one call (var btn1 = new Button(..)) is now two calls (var btn1 = Object.create(Button) and btn1.setup(..)). Initially this may seem like a drawback (more code).

However, even this is something that’s a pro of OLOO style code as compared to classical prototype style code. How?

With class constructors, you are “forced” (not really, but strongly suggested) to do both construction and initialization in the same step. However, there are many cases where being able to do these two steps separately (as you do with OLOO!) is more flexible.

For example, let’s say you create all your instances in a pool at the beginning of your program, but you wait to initialize them with specific setup until they are pulled from the pool and used. We showed the two calls happening right next to each other, but of course they can happen at very different times and in very different parts of our code, as needed.

OLOO supports better the principle of separation of concerns, where creation and initialization are not necessarily conflated into the same operation.