Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 pass function as parameter example

I don't know JS/ES6 well enough to describe my question in code. So most of this question is conceptually and in pseudo code.

Say I have a Contractor class like this:

class Contractor {
 constructor(jobFn) {
    // save jobFn;
  }

  dailyRoutine() {
    // let result = DriveToWork()
    const result = 6
    DoTheJob(result)
    DriveBackHome()
  }

}

The problem is, what the DoTheJob() does might be different things in different places.

So in place A, it could be

he = new Contractor(write_front_end(with, this, and that))

And in place B, it could be

he = new Contractor(fix_backend_node(with, express))

I.e., the behavior need to be passed in during the constructor, and the action might need to take different kind and different amount of parameters.

Would such thing be possible with ES6?
Please show ES6 code that can pass function with different kind and different amount of parameters through the constructor to DoTheJob().

Further, the challenge is that the jobFn need to be a Curried function, meaning there is one or more parameter missing to do the DoTheJob job. Say if the jobFn is passed with Curried add(3), then DoTheJob will do UncurriedAdd of add(3, 6); if then jobFn is passed with Curried multiple(5), then DoTheJob will do Uncurried of multiple(5, 6);

like image 369
xpt Avatar asked Jan 27 '19 03:01

xpt


People also ask

Can we pass function as a parameter in js?

Conclusion. Functions in the functional programming paradigm can be passed to other functions as parameters. These functions are called callbacks. Callback functions can be passed as arguments by directly passing the function's name and not involving them.

Can you use a function as a parameter of another function?

Functions, like any other object, can be passed as an argument to another function.


1 Answers

Just assign the passed function to this.DoTheJob, and then call this.DoTheJob inside dailyRoutine:

class Contractor {
  constructor(jobFn) {
    this.DoTheJob = jobFn;
  }
  dailyRoutine() {
    // DriveToWork()
    this.DoTheJob();
    // DriveBackHome()
  }
}

const c1 = new Contractor(() => console.log('doing job A'));
c1.dailyRoutine();

const c2 = new Contractor(() => console.log('doing job B'));
c2.dailyRoutine();

// c1 again:
c1.dailyRoutine();

// feel free to reference any in-scope variables in the passed function,
// no need to pass the variables as additional parameters
const data = 'data';
const c3 = new Contractor(() => console.log('data is', data));
c3.dailyRoutine();

If dailyRoutine needs to be invoked with data that needs to be sent to the passed doTheJob function, just define the needed arguments in the function you pass, there's no need for actual currying here:

class Contractor {
  constructor(jobFn) {
    this.DoTheJob = jobFn;
  }
  dailyRoutine(doJobArg) {
    this.DoTheJob(doJobArg);
  }
}

// feel free to reference any in-scope variables in the passed function,
// no need to pass the variables as additional parameters
const data = 'data';
const c3 = new Contractor((arg) => console.log('data is', data, 'and arg is', arg));
c3.dailyRoutine('argDoTheJobIsCalledWith');
like image 78
CertainPerformance Avatar answered Oct 12 '22 23:10

CertainPerformance