Passing function definition into callback function without affecting signature

175 Views Asked by At

I am trying to create a chaining system where I need to be able to access a function definition from within the callback to a chained function.

Below is the class and instantiation test.

What I need to be able to do is call the "func" method from within the .next callback without affecting the signature of the callback.

I need the "func" method to be able to also reference the current instance of the Test class.

Is there a way I can pass a definition of an object/function through to the callback function without changing the callback signature?

let Test = class {
    constructor( executor ) {
        executor( this.func );
    }

    func() {
        this.value = 'test';
    }

    next( cb ) {
        cb();
        return this;
    }
}

let t1 = new Test(
        ( func ) => {
            func();
        }
    )
    .next(
        ( arg1, arg2 ) => {
            console.log( 'func' );
            console.log( func );
        }
    );
0

There are 0 best solutions below