Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否接受带有参数的javascript函数作为参数(无包装器)?_Javascript - Fatal编程技术网

是否接受带有参数的javascript函数作为参数(无包装器)?

是否接受带有参数的javascript函数作为参数(无包装器)?,javascript,Javascript,是否可以在没有周围的“包装器”匿名函数()的情况下编写此函数? 所以,基本上把runner(function(){myfunction('hello world')},'Works!!!')变成这个runner(myfunction('hello world'),'Works!!!') JS function runner(func, something) { alert(something); func(); } function myfunction(value) {

是否可以在没有周围的“包装器”匿名函数()的情况下编写此函数? 所以,基本上把
runner(function(){myfunction('hello world')},'Works!!!')
变成这个
runner(myfunction('hello world'),'Works!!!')

JS

function runner(func, something)
{
    alert(something);
    func();
}


function myfunction(value)
{
    alert("I should execute last!");
}
HTML

<button onclick="javascript: runner(function(){ myfunction('hello world') }, 'Works!!!')">Hit me</button>
打我

JS FIDDLE

事实证明,您毕竟可以这样做:-)您可以使用创建一个新的函数对象,第一个参数作为当前上下文,其余参数作为实际函数的参数

function runner(func, something) {
    console.log(something);
    func();
}

function myfunction(value) {
    console.log(value);
    console.log("I should execute last!");
}

runner(myfunction.bind(this, 'hello world'), 'Works!!!')
输出

Works!!!
hello world
I should execute last!

runner
函数需要一个不带参数的函数,因为它是这样运行的:

func();
runner(myfunction, 'Works!!!')
因此,您不能像这样将
myfunction
传递给它:

func();
runner(myfunction, 'Works!!!')
因为你没有办法给
myfunction
一个参数(从技术上讲,它确实有效,因为你的
myfunction
根本不使用参数)。但如果你尝试这样做:

runner(myfunction('hello world'), 'Works!!!')
然后,您所做的就是直接调用
myfunction
,并将返回值(在本例中为
undefined
)作为参数发送给
runner
,如果查看控制台,您应该会看到一个错误:
TypeError:undefined不是函数


因此,不需要,您需要原始调用中的包装器,或者您可以按照前眼的建议使用
Function.prototype.bind

您还可以使用apply或call,以便:

<button onclick="javascript: runner(myfunction,'hello world', 'Works!!!')">Hit me</button>

function runner(func, args, something)
{
    alert(something);
    func.call(this, args);
}

function myfunction(value)
{
    alert("I should execute last!");
}
打我
函数运行程序(func、args、something)
{
警惕(某事);
函数调用(this,args);
}
函数myfunction(值)
{
警惕(“我应该最后执行!”);
}
是否可以在没有周围的“包装器”匿名函数()的情况下编写此函数

要拥有预设参数,您需要以某种方式使用包装器函数。不需要内联声明它是很方便的,但是所有的解决方案仍然需要使用包装器函数

调用在函数执行之前为其预先设置参数的功能。基本上,这个概念是调用一个函数来生成一个新函数。新函数将使用正确的参数调用原始函数

香草JavaScript 允许在上下文之后传递其他参数,在最终调用函数时将使用这些参数:

runner(myfunction.bind(window, 'hello world'), 'Works!!!');
当然,IE8及以下版本不支持此功能,因此需要使用polyfill来启用此行为

jQuery jQuery库中的跨浏览器兼容版本:

runner($.proxy(myfunction, window, 'hello world'), 'Works!!!');
强调 下划线JS库中的跨浏览器兼容版本:

runner(_.bind(myfunction, window, 'hello world'), 'Works!!!');
但是,如果要避免在生成包装时绑定上下文,下划线还提供了真正的部分应用程序函数

function runner(func, something) {
    console.log(something);
    func();
}

function myfunction(value) {
    console.log(value);
    console.log("I should execute last!");
}

runner(myfunction.bind(this, 'hello world'), 'Works!!!')
将仅绑定参数,并允许在执行函数时确定上下文:

runner(_.partial(myfunction, 'hello world'), 'Works!!!');

从技术上讲,bind调用仍然会生成一个包装器函数,但至少它不是内联的。O@zzzzBov对的我在回答中也提到:)@thefourtheye它只使用“.bind”,而不使用“this”:-@BakedInhalf如果你不需要传递任何参数,那么
bind
本身就可以了:)你要找的是
Function.prototype.bind
可以绑定参数,但类似的方式可能更合适。如果下面的答案之一解决了您的问题,您应该接受它(单击相应答案旁边的复选标记)。这有两件事。它让每个人都知道你的问题已经解决了,并让帮助你的人相信你的帮助。获取完整的解释。如果您所做的只是使用一个额外的参数,那么为什么要在那里使用
调用
?不妨使用
func(args)
@Cerbrus:答案解释了为什么没有。尽管正如第四位作者所指出的,有一种方法可以使用
函数.prototype.bind
,尽管它本质上与包装它相同,但语法不同。@Cerbrus你是对的,但这就是为什么我首先使用了包装器。我在想办法跳过它。更改“runner”或类似的内容