Piotr Dajlido
Piotr Dajlido

Reputation: 2030

JS delegating/extending aleady existing function

How to add code to the function preserving it's old functionality

SomeFunction = function () {
    someOtherFunction();
    return this.apply(this, arguments);
};

Upvotes: 0

Views: 32

Answers (1)

corneliusz
corneliusz

Reputation: 108

Just like that

var delegate = functionToBeDelegated;
functionToBeDelegated = function () {

    //
    // Add functionality to the function
    //

    // run the old version of the function in native scope
    return delegate.apply(this, arguments);
};

Upvotes: 2

Related Questions