0

For example. I have this code:

export const mergeWrapper = function(aFunction, meta) {
  return aFunction;
};

function exampleFunction(temp) {
  console.log(temp);
  // can print here. without changing exampleFunction method signature.
  // in this case should be 'stackoverflow'
  console.log(meta);  
}

mergeWrapper(exampleFunction, 'stackoverflow')('hello');

I want to do "something" in mergeWrapper. so that, inside aFunction, I can call meta. I have tried something such as:

export const mergeWrapper = function(aFunction, meta) {
  aFunction.bind(this);
  return aFunction;
};

But it doesn't work. How can I do this.

My idea because I can write some sort of this function using currying. for example:

export const wrapperFunction = function(meta) {
   return function(temp) {
       console.log(temp);
       console.log(meta);
   }
}
wrapperFunction('StackOverFlow')('hello');

But written like this will make me write wrapper for all functions. So I want to write a helper.

Thanks

Trần Kim Dự
  • 5,872
  • 12
  • 55
  • 107
  • This looks like an XY problem. What do you intend to use this for? – melpomene Sep 23 '17 at 21:01
  • @melpomene because I have shorten again my problem. I want to wrap my function inside "environment". I can write a wrapper function and using currying. But if I do that, I will write wrapper function manually for all need functions. So I want to implement a wrapper like above. – Trần Kim Dự Sep 23 '17 at 21:03
  • In general, you can't do this because JavaScript doesn't support dynamic scoping. However, as the answer below suggests you could always pass the `meta` as an argument to the function. Another way is to use `this.meta` inside your function instead. – Aadit M Shah Sep 23 '17 at 21:06
  • @melpomene edited my question. Please take a look. – Trần Kim Dự Sep 23 '17 at 21:07
  • @AaditMShah can you see my question again ? I have edited. I don't think it has duplicated. – Trần Kim Dự Sep 23 '17 at 21:08
  • Still don't see what you're trying to do. Your `wrapperFunction` isn't too bad. You can shorten it to `export const wrapperFunction = meta => temp => { console.log(temp); console.log(meta); };` As for duplication, it's not always bad. – Aadit M Shah Sep 23 '17 at 21:10
  • You can't do that, but you haven't provided enough context to suggest an alternative. I'll just note that you can shorten your wrapper definition quite a bit: `export const wrapperFunction = meta => temp => { console.log(temp); console.log(meta); };` – melpomene Sep 23 '17 at 21:11
  • thanks for your nice syntax. Because I think aFunction is doing a business X, i don't want to change its signature for supporting some business Y. If we can write an independent function that can add "something more" (in here, wrapper function), that would be better. that's my idea. – Trần Kim Dự Sep 23 '17 at 21:14
  • This is still too vague. What it boils down to me is: You want to use a parameter (`meta`) but you don't want to add it to the parameter list for some reason. I don't understand why. – melpomene Sep 23 '17 at 21:17

1 Answers1

0

You can do

const mergeWrapper = function(aFunction, meta) {
  return aFunction.bind(null, meta);
};

function exampleFunction(meta, temp) {
  console.log(temp);
  // can print here. without changing exampleFunction method signature.
  // in this case should be 'stackoverflow'
  console.log(meta);  
}

let func = mergeWrapper(exampleFunction, 'stackoverflow');
func('temp');
func('temp2');

Where you have binded meta as the first argument, and all calls would get that value of meta

marvel308
  • 10,288
  • 1
  • 21
  • 32