Javascript 使用另一个函数中的参数调用函数
我试图构建一种调度器函数,能够在随机时刻调用另一个具有参数的函数。 以下是我的尝试,使用Javascript:Javascript 使用另一个函数中的参数调用函数,javascript,function,parameter-passing,Javascript,Function,Parameter Passing,我试图构建一种调度器函数,能够在随机时刻调用另一个具有参数的函数。 以下是我的尝试,使用Javascript: function scheduleFunction(t, deltaT, functionName) { var timeout; var timeoutID; function scheduler() { functionName() clearTimeout(timeoutID); timeout = Math.tr
function scheduleFunction(t, deltaT, functionName) {
var timeout;
var timeoutID;
function scheduler() {
functionName()
clearTimeout(timeoutID);
timeout = Math.trunc(Math.random() * 2 * deltaT - deltaT) + t;
timeoutID = setTimeout(scheduler, timeout);
}
scheduler();
}
如果我让这个函数调用另一个不需要参数的函数,它就可以正常工作。例如:
function printSomething() {
console.log("Printing something...");
}
scheduleFunction(1000, 500, printSomething);
不幸的是,该函数不允许使用参数调用另一个函数,例如:
function print(string) {
console.log(string);
}
scheduleFunction(1000, 500, print("Hello World!"));
如果可能的话,我应该如何编辑调度程序函数以获得这种结果?您可以绑定参数:
scheduleFunction(1000, 500, print.bind(null, "Hello World!"));
function scheduleFunction(t、deltaT、functionName){
var超时;
var-timeoutID;
函数调度程序(){
函数名()
clearTimeout(timeoutID);
timeout=Math.trunc(Math.random()*2*deltaT-deltaT)+t;
timeoutID=setTimeout(调度程序,超时);
}
调度程序();
}
函数打印(字符串){
console.log(字符串);
}
scheduleFunction(1000500,print.bind(null,“helloworld!”)代码>您可以绑定参数:
scheduleFunction(1000, 500, print.bind(null, "Hello World!"));
function scheduleFunction(t、deltaT、functionName){
var超时;
var-timeoutID;
函数调度程序(){
函数名()
clearTimeout(timeoutID);
timeout=Math.trunc(Math.random()*2*deltaT-deltaT)+t;
timeoutID=setTimeout(调度程序,超时);
}
调度程序();
}
函数打印(字符串){
console.log(字符串);
}
scheduleFunction(1000500,print.bind(null,“helloworld!”)代码>简单
scheduleFunction(1000, 500, function() {
print("Hello World!")
});
简单的
调用函数时,会将类似数组的对象变量called添加到函数范围中
它保存实际传递到函数中的参数,而不仅仅是对象定义中定义的参数。这意味着无论传入的参数是少是多,都可以访问传入的所有参数
知道了这一点,您可以将从第4个参数开始传递到scheduledFunction
的任何参数视为将用于传递到functionName
的参数。这可以通过使用and实现(必需,因为参数不是数组,而是类似数组的对象):
然后,可以使用调用functionName
将切片参数作为参数传递:
functionName.apply(null, args);
修改后的代码将如下所示:
function scheduleFunction(t, deltaT, functionName) {
var timeout;
var timeoutID;
// Get the rest of the parameters (if any)
// from the 4th one onwards
var args = [].slice.call(arguments, 4);
function scheduler() {
// Call functionName, passing in all the extra parameters
functionName.apply(null, args);
// Etc...
}
scheduler();
}
像这样使用它,请注意,print
不再被调用,但传递给它的参数只是作为scheduleFunction
的参数跟随它:
function print(string) {
console.log(string);
}
scheduleFunction(1000, 500, print, "Hello World!");
通过修改console.log的调用方式,可以打印多个参数:
function print() {
// No parameters defined BUT arguments is
// still available.
// Obtain a real array from it using slice
var args = [].slice.call(arguments, 0);
// Use Function#apply to call console log with
// multiple parameters from args array
console.log.apply(null, args);
}
scheduleFunction(1000, 500, print, "Hello World!, "Hello again!");
调用函数时,会将类似数组的对象变量called添加到函数范围中
它保存实际传递到函数中的参数,而不仅仅是对象定义中定义的参数。这意味着无论传入的参数是少是多,都可以访问传入的所有参数
知道了这一点,您可以将从第4个参数开始传递到scheduledFunction
的任何参数视为将用于传递到functionName
的参数。这可以通过使用and实现(必需,因为参数不是数组,而是类似数组的对象):
然后,可以使用调用functionName
将切片参数作为参数传递:
functionName.apply(null, args);
修改后的代码将如下所示:
function scheduleFunction(t, deltaT, functionName) {
var timeout;
var timeoutID;
// Get the rest of the parameters (if any)
// from the 4th one onwards
var args = [].slice.call(arguments, 4);
function scheduler() {
// Call functionName, passing in all the extra parameters
functionName.apply(null, args);
// Etc...
}
scheduler();
}
像这样使用它,请注意,print
不再被调用,但传递给它的参数只是作为scheduleFunction
的参数跟随它:
function print(string) {
console.log(string);
}
scheduleFunction(1000, 500, print, "Hello World!");
通过修改console.log的调用方式,可以打印多个参数:
function print() {
// No parameters defined BUT arguments is
// still available.
// Obtain a real array from it using slice
var args = [].slice.call(arguments, 0);
// Use Function#apply to call console log with
// multiple parameters from args array
console.log.apply(null, args);
}
scheduleFunction(1000, 500, print, "Hello World!, "Hello again!");
您可以将第四个参数传递给scheduleFunction,以用作传递函数的参数。您可以将第四个参数传递给scheduleFunction,以用作传递函数的参数。只需将带有参数的函数封装在另一个没有参数的函数中,不是吗?只是把一个有参数的函数封装在另一个没有参数的函数中,不是吗?