admin管理员组

文章数量:1401591

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);

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);

Share Improve this question edited Jan 27, 2019 at 4:30 xpt asked Jan 27, 2019 at 3:50 xptxpt 23.2k45 gold badges154 silver badges245 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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');

In my case, I may advise you that it's better to give the predicate to dailyRoutine, because this way you'll be able to reuse the same instance and give different predicates.

Anyway, there's a pure OOP solution for this, using method polymorphism, the JavaScript way (aka duck typing):

class Contractor {
  driveBackHome() {}

  dailyRoutine() {
    const result = 6

    this.doTheJob(result)
    this.driveBackHome()
  }
}

class SpecializedContractorA extends Contractor {
  doTheJob(result) {
    console.log('aaaaaaaaaaaaaaaaaaaaa', result)
  }
}

class SpecializedContractorB extends Contractor {
  doTheJob(result) {
    console.log('bbbbbbbbbbbbbbbb', result)
  }
}

const a = new SpecializedContractorA()
a.dailyRoutine()

const b = new SpecializedContractorB()
b.dailyRoutine()

本文标签: javascriptES6 pass function as parameter exampleStack Overflow