Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to Re-Architect a JavaScript Class System

Olga
May 13, 2019

How to Re-Architect a JavaScript Class System

Olga

May 13, 2019
Tweet

More Decks by Olga

Other Decks in Programming

Transcript

  1. Olga Petrova, @tyoushe Inheritance via Prototyping function MyComponent() {} MyComponent.prototype.init

    = function() { //... } function MyChildComponent() {} MyChildComponent.prototype = Object.create(new MyComponent()); MyChildComponent.prototype.destroy = function() { //... } var cmp = new MyChildComponent(); cmp.init(); cmp.destroy();
  2. Olga Petrova, @tyoushe Ext JS Class System ✔ Inheritance ✔

    Config properties ✔ Dependencies ✔ Mixins ✔ Overrides ✔ Build tool
  3. Olga Petrova, @tyoushe Ext JS Class Ext.define('My.own.Component', { extend: 'Ext.Component',

    requires: ['My.Component.Dependency'], myProperty: true, myMethod: function() { //... this.callParent(arguments); }, statics: { staticMethod: function() { //... } } });
  4. Olga Petrova, @tyoushe Ext JS Config Properties Ext.define('My.own.Component', { extend:

    'Ext.Component‘, config: { myConfig: 'My Text' }, applyMyConfig: function(newTitle, oldTitle) { //implement validation return newTitle; }, updateMyConfig: function(newTitle, oldTitle) { //implement side effects } });
  5. Olga Petrova, @tyoushe Ext JS Overrides var MyComponent = new

    Ext.Component({}); Ext.override(MyComponent, { myProperty: 'My Text', myMethod: function () { //... } });
  6. Olga Petrova, @tyoushe Ext JS Mixins Ext.define('Mixin‘, { processData: function(data)

    { /**/ } }); Ext.define('MyComponent‘, { mixins: { myMixin: 'Mixin' }, processData: function(data) { //... this.mixins.myMixin.processData.call(this); } });
  7. Olga Petrova, @tyoushe 10K CUSTOMERS WORLDWIDE 2M SENCHA DEVS WORLDWIDE

    7.2M PRODUCT DOWNLOADS 500K ACTIVE FORUM MEMBERS 60% of Fortune 100 Companies Rely on Sencha
  8. Olga Petrova, @tyoushe ECMAScript6 Class Systems ✔ Inheritance ✔ Dependencies

    ✔ Build tool ❌ Config properties ❌ Mixins ❌ Overrides
  9. Olga Petrova, @tyoushe Decorator pattern is a design pattern that

    allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class
  10. Olga Petrova, @tyoushe @wrap Method class C { @wrap(f) method()

    { } } class C { method() { } } C.prototype.method = f(C.prototype.method); =>
  11. Olga Petrova, @tyoushe ECMAScript 6 to Ext JS Class Systems

    ✔ Inheritance ✔ Dependencies ✔ Build tool ✔ Config properties @define for class & @config for class fields ✔ Mixins @define for class ✔ Overrides @override for class
  12. Olga Petrova, @tyoushe Class @define({ prototype: { myProperty: "My text"

    }, static: { myStaticProperty: 42 } }) class MyOwnComponent extends Component { // }
  13. Olga Petrova, @tyoushe Configs @define class MyOwnComponent extends Component {

    @config('nullify') myConfig = 42; applyMyConfig (value, oldValue) { //validation return value; } updateMyConfig (value, oldValue) { //side effects } }
  14. Olga Petrova, @tyoushe Mixins @define class Mixin extends Base {

    processData (data) {...} } @define({ mixins: [ Mixin ] }) class MyComponent extends Component { @junction processData (data) { super.processData(data); } }
  15. Olga Petrova, @tyoushe Overrides class MyComponent extends Component {} @override(MyComponent)

    class MyComponentOverride { myProperty = ‘My Text’; myMethod () { //... } }
  16. Olga Petrova, @tyoushe Life-cycle Methods class MyComponent extends Component {

    ctor () { // do constructor-like things } setup() { // finalize configuration } dtor () { // do destructor-like things } }