1

My goal is to execute a javascript snippet using another javascript snippet using

new Function(x1, x2, x3, functionBody) call.

My problem appears when i need to pass parameters to the function, this is because functionBody may appear as a new Js script with global declarations and calls.

function main() {
var a = x1;
var b = x2;
var c = x3;
....
....
}

main(); // this is the function that starts the flow of the secondary Js snippets

Edit: I have a script responsible for downloading and executing another Js script. each downloaded script is executed by a global call to main(), which is unknown to the caller script.

igal k
  • 1,883
  • 2
  • 28
  • 57
  • 4
    You're suggesting a completely unconventional solution to a problem you haven't even described to us. All I'm hearing is "these wires don't connect" but you somehow failed to tell everyone that we're trying to defuse a bomb. – Mulan Aug 17 '15 at 08:26
  • 1
    Zoom out. What is the actual goal? – Mulan Aug 17 '15 at 08:31
  • @naomik: i have edited the post, thanks! – igal k Aug 17 '15 at 08:36
  • I don't know I got the sense of the question, but to me it looks like that your are looking for a way to pass parameters to callback. I think this questions and answers might give you a hint: http://stackoverflow.com/questions/3458553/javascript-passing-parameters-to-a-callback-function – jjczopek Aug 17 '15 at 08:40
  • @jjczopek, In the link you provided me with, they used the 'Arguments' known variable, however, in my example, the secondary script, starts from the global namespace, which from my tests, didn't recognize the 'Arguments' variable – igal k Aug 17 '15 at 08:47
  • I do not understand why the `wsh` tag has been used here. Is this a JScript classic question? If true then the `jscript` tag should be used. – Foad S. Farimani Oct 31 '20 at 23:32

1 Answers1

0

You seem to be misunderstanding how the Function constructor works.

// This does not create a function with x1, x2, x3 parameters
new Function(x1, x2, x3, functionBody)

// This does 
new Function('x1', 'x2', 'x3', functionBody)

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function#Specifying_arguments_with_the_Function_constructor

// Create a function that takes two arguments and returns the sum of those arguments
var adder = new Function('a', 'b', 'return a + b');

// Call the function
adder(2, 6);
// > 8
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217