-1

I want to write one wrapper for a javascript file and want to add more parameters to a function through the wrapper. How do I do that?

Leo
  • 31
  • 4
  • Isn't that just a new function that calls the old function, and does something with the extra arguments in the meantime? Can you be more specific? Are you just passing defaults to the wrapped function, or trying to change the behaviour of the wrapped function (i.e. do you actually need to edit it instead) or something else? As it stands I'm not clear precisely what you're trying to do. – Rup May 31 '18 at 07:11
  • I need to change the old function so I am writing a wrapper. Old function is in one javascript file which I cannot edit. I need to pass more parameters in that old function using a wrapper. – Leo May 31 '18 at 09:04
  • OK, but I'm still not sure exactly what you mean by "I need to pass more parameters in that old function". Are you passing more parameters than the original function accepts, or fewer? If it's more, what are you going to do with the extra parameters? If it's less, I assume you're just calling the wrapped function with defaults. – Rup May 31 '18 at 09:06
  • In javascript you can call function as many parameters as you want, more than that function was declared to have, and you can use those parameters inside that function as well, it doesn't matter if you didn't declare those parameters, they will be present inside args array. But if you can't change that function then it doesn't help you. – Milan Živković May 31 '18 at 09:09
  • I cannot change the original file so I have to write a wrapper for that. I want to pass more parameters into the original function through the wrapper. – Leo May 31 '18 at 09:10

1 Answers1

1

Just create new function with all parameters that you need (including the parameters that the wrapped function needs), do what you need to do with new parameters and use the old ones to call the wrapped function:

function multiply(a, b){
    return a * b;
}

// example: a=2, b=3, c="The result of multiplication is: "
function multiplyAsTextMessage(a, b, c){
    return c + String(multiply(a, b)); // calling wrapped function
}

Is this what you were searching for?

  • Can you please add simple code representing your answer? – Leo May 31 '18 at 08:53
  • Yes. I got the idea how to proceed. Thank you. – Leo May 31 '18 at 09:18
  • What if (taking your example) multiply function is in another file and I want to include that file in my wrapper file? – Leo Jun 01 '18 at 06:00
  • Just include that file in your javascript file that hold your wrapper. To see how to include js file, take a look at this link: https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file – Milan Živković Jun 01 '18 at 07:07
  • I have tried that and there is this error - funOne is not a function. – Leo Jun 01 '18 at 09:10
  • Did you do this line of code: import {funOne} from 'name-of-original-js-file'? – Milan Živković Jun 01 '18 at 09:37