An experiment using an object literal based class definition pattern

Bob Nystrom rnystrom at google.com
Fri Aug 5 16:15:34 PDT 2011


On Fri, Aug 5, 2011 at 3:29 PM, Allen Wirfs-Brock <allen at wirfs-brock.com>wrote:

> I think the Class function I suggest above could be an answer.  But
> exploring this was my motivation in writing a significant amount of code
> using my pattern.  I personally didn't find it awkward, error-prone, or to
> present any readability issues.  But that is all subjective.  If somebody
> should translated that corpus into class declarations we can do a
> side-by-side comparison of readability.
>

Brilliant idea. I started doing that
here<https://github.com/munificent/ESnext-experiments>.
Example:


  // Define a non-constructible superclass that provides some Smalltalk-like
  // conventions.
  class AbstractClass {
    name = "AbstractClass";


    get class() { return this.constructor; }

    error(message) { throw new Error(message); }

    subclassResponsibility() { this.class.subclassResponsibility(); }
    shouldNotImplement() { this.class.shouldNotImplement(); }

    errorSubscriptBounds(index) {
      this.error("subscript is out of bounds: " + index);
    }

  class:
    subclassResponsibility() {
      throw new Error(this.name + " did not implemente an abstract method.");
    }

    shouldNotImplement() {
      throw new Error(this.name + " should not implemented by a method");
    }
  }

- bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110805/817c8022/attachment.html>


More information about the es-discuss mailing list