Skip to content

Instantly share code, notes, and snippets.

@jordaaash
Created October 18, 2016 00:29
Show Gist options
  • Save jordaaash/a340bf42c2d00b397c8e9081cbe924ae to your computer and use it in GitHub Desktop.
Save jordaaash/a340bf42c2d00b397c8e9081cbe924ae to your computer and use it in GitHub Desktop.

Revisions

  1. jordaaash created this gist Oct 18, 2016.
    22 changes: 22 additions & 0 deletions DecoratorPattern.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    // @flow

    class SuperClass {
    something () {
    }
    }

    class SubClass extends SuperClass {
    }

    function decorateWithSomethingElse <T: Class<SuperClass>> (SuperClass: T): T {
    return class SomethingElseAdded extends SuperClass {
    addSomethingElse () {
    }
    }
    }

    var SubClassWithSomethingElse = decorateWithSomethingElse(SubClass)

    // (new SubClass) instanceof SuperClass -> true
    // (new SubClassWithSomethingElse) instanceof SubClass -> true
    // (new SubClassWithSomethingElse) instanceof SuperClass -> true