如何在评估时将参数/参数从一个 javascript 片段传递到另一个片段
How to pass parameters / arguments from one javascript snippet to another, when evaluated
我的目标是使用
使用另一个 javascript 片段执行 javascript 片段
new Function(x1, x2, x3, functionBody)
来电.
当我需要将参数传递给函数时,我的问题出现了,这是因为 functionBody 可能会显示为具有全局声明和调用的新 Js 脚本。
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
编辑:我有一个脚本负责下载并执行另一个Js脚本。每个下载的脚本都是通过对 main() 的全局调用执行的,调用者脚本不知道它。
您似乎误解了 Function
构造函数的工作原理。
// 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)
// 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
我的目标是使用
使用另一个 javascript 片段执行 javascript 片段new Function(x1, x2, x3, functionBody)
来电.
当我需要将参数传递给函数时,我的问题出现了,这是因为 functionBody 可能会显示为具有全局声明和调用的新 Js 脚本。
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
编辑:我有一个脚本负责下载并执行另一个Js脚本。每个下载的脚本都是通过对 main() 的全局调用执行的,调用者脚本不知道它。
您似乎误解了 Function
构造函数的工作原理。
// 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)
// 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