[英]Javascript - add parameters to a function passed as a parameter
Here is the behaviour I'm looking for:这是我正在寻找的行为:
function one(func){
func(5);
}
function two(arg1, arg2){
console.log(arg1);
console.log(arg2);
}
one(two(3)) //prints 3, 5
Can this behaviour or something similar be accomplished in javascript?可以在javascript中完成这种行为或类似的事情吗?
You can always use the bind() function to pass some arguments to your function.您始终可以使用bind()函数将一些参数传递给您的函数。 It'll create a new function with the first argument - arg1 - equal to the value of 3 in this example:
它将创建一个新函数,其第一个参数 - arg1 - 在此示例中等于3的值:
function one(func){
func(5);
}
function two(arg1, arg2){
console.log(arg1);
console.log(arg2);
}
one(two.bind(null, 3))
You can read more about the bind() function here: MDN - Bind您可以在此处阅读有关bind()函数的更多信息: MDN - Bind
Some workaround is possible一些解决方法是可能的
function one() { var args = Array.prototype.slice.call(arguments); var func = args[0]; args.splice(0, 1); args.push(5); func.apply(this, args); } function two(arg1, arg2) { console.log(arg1); console.log(arg2); } one(two, 3)
There's a problem with your syntax: function one
is expecting its single argument to be a function.您的语法有问题:第
one
函数期望它的单个参数是一个函数。 Then, below, when you invoke it, you are not passing the function two
, but whatever two
returns when it's passed a single argument, probably undefined
.然后,在下面,当您调用它时,您并没有传递函数
two
,而是传递单个参数时返回的任何two
,可能是undefined
。 I don't know what specifically you're trying to accomplish but I'd recommend a little research into closures .我不知道您具体想要完成什么,但我建议您对闭包进行一些研究。
as soon as one
expects function as argument two(3)
should return function.只要
one
希望充当论据two(3)
应返回的功能。
this condition is required so in order to achieve it your two
function should be这个条件是必需的,所以为了实现它,你的
two
功能应该是
function two(arg1){
console.log(arg1);
return function(arg2) {
console.log(arg2);
};
}
so two(3)
function call gets passed as argument to one
所以
two(3)
函数调用被作为参数传递给one
so before assigning value to variable engine executes it.所以在赋值给变量引擎之前执行它。 And execution of
two(3)
call logs 3
to console and returns function并执行
two(3)
调用日志3
到控制台并返回函数
function(arg2) {
console.log(arg2);
};
and then engine assigns executed value(returned function) to func
variable.然后引擎将执行的值(返回的函数)分配给
func
变量。 so func
parameter of one
function now looks like所以
one
函数的func
参数现在看起来像
func = function(arg2) {
console.log(arg2);
};
one
calls func
with 5
passed in as argument. one
调用func
并传入5
作为参数。 so 5
gets logged to console.所以
5
被记录到控制台。
function one(arg){
two(arg, 5); // func here is two so it requires two params...
}
function two(arg1, arg2){
console.log(arg1);
console.log(arg2);
}
one(3)// one expect function so can't execute function here!
Basically you can't specify the parameter in the function or it'll run.基本上你不能在函数中指定参数,否则它会运行。 You need to specify the function aka one(two), but that obviously wouldn't work.
您需要指定函数又名一(二),但这显然行不通。
However if you dynamically create a function you should be able to accomplish the task like so :但是,如果您动态创建一个函数,您应该能够像这样完成任务:
function one(func){
func(5);
}
function two(arg1, arg2){
console.log(arg1);
console.log(arg2);
}
one(function(val) { two(3, val) }) //prints 3, 5
well somehow this things work for me好吧,这东西对我有用
function one(func){ console.log("one is running"); func; } function two(args1, args2){ console.log("two is running"); console.log("args1 -> " + args1); console.log("args2 -> " + args2); } //to call it args1 = 6; args2 = 12 one(two(args1,args2));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.