• Helper function when creating dynamic (inline) functions for classes, this helper performs the following tasks :-

    • Saves references to all defined base class functions
    • Calls the delegateFunc with the current target (this) and a base object reference that can be used to call all "super" functions.
    • Will populate the class prototype for all overridden functions to support class extension that call the prototype instance. Callers should use this helper when declaring all function within the constructor of a class, as mentioned above the delegateFunc is passed both the target "this" and an object that can be used to call any base (super) functions, using this based object in place of super.XXX() (which gets expanded to _super.prototype.XXX()) provides a better minification outcome and also ensures the correct "this" context is maintained as TypeScript creates incorrect references using super.XXXX() for dynamically defined functions i.e. Functions defined in the constructor or some other function (rather than declared as complete typescript functions).

    Usage

    import dynamicProto from "@microsoft/dynamicproto-js";
    class ExampleClass extends BaseClass {
    constructor() {
    dynamicProto(ExampleClass, this, (_self, base) => {
    // This will define a function that will be converted to a prototype function
    _self.newFunc = () => {
    // Access any "this" instance property
    if (_self.someProperty) {
    ...
    }
    }
    // This will define a function that will be converted to a prototype function
    _self.myFunction = () => {
    // Access any "this" instance property
    if (_self.someProperty) {
    // Call the base version of the function that we are overriding
    base.myFunction();
    }
    ...
    }
    _self.initialize = () => {
    ...
    }
    // Warnings: While the following will work as _self is simply a reference to
    // this, if anyone overrides myFunction() the overridden will be called first
    // as the normal JavaScript method resolution will occur and the defined
    // _self.initialize() function is actually gets removed from the instance and
    // a proxy prototype version is created to reference the created method.
    _self.initialize();
    });
    }
    }

    Typeparam

    DPType This is the generic type of the class, used to keep intellisense valid

    Typeparam

    DPCls The type that contains the prototype of the current class

    Type Parameters

    • DPType

    • DPCls

    Parameters

    • theClass: DPCls

      This is the current class instance which contains the prototype for the current class

    • target: DPType

      The current "this" (target) reference, when the class has been extended this.prototype will not be the 'theClass' value.

    • delegateFunc: DynamicProtoDelegate<DPType>

      The callback function (closure) that will create the dynamic function

    • Optional options: IDynamicProtoOpts

      Additional options to configure how the dynamic prototype operates

    Returns void

Generated using TypeDoc