Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.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 Function.prototype.bind.apply()不';I don’我没有按预期工作_Javascript_Functional Programming_Function Binding - Fatal编程技术网

Javascript Function.prototype.bind.apply()不';I don’我没有按预期工作

Javascript Function.prototype.bind.apply()不';I don’我没有按预期工作,javascript,functional-programming,function-binding,Javascript,Functional Programming,Function Binding,我有一个函数myfunc,想bind将它绑定到一个特定的this参数和bind的其他参数,作为一个数组,而不是参数列表(因为我将参数列表作为函数的参数,在这里执行此代码)。 为此,我在bind上使用apply,如下所示: var myfunc = function(arg1, arg2){ alert("this = " + this + ", arg1 = " + arg1 + ", arg2 = " + arg2); } var bindedMyfunc = myfunc.bind.

我有一个函数
myfunc
,想
bind
将它绑定到一个特定的
this
参数和
bind
的其他参数,作为一个数组,而不是参数列表(因为我将参数列表作为函数的参数,在这里执行此代码)。 为此,我在
bind
上使用
apply
,如下所示:

var myfunc = function(arg1, arg2){
    alert("this = " + this + ", arg1 = " + arg1 + ", arg2 = " + arg2);
}
var bindedMyfunc = myfunc.bind.apply("mythis", ["param1", "param2"]);
bindedMufunc();
这将导致
未捕获类型错误:必须对函数调用Bind

我做错了什么?你能详细解释一下,当我运行这段代码时发生了什么,因为现实似乎与我的观点相矛盾

答案摘要: 似乎
bind
本身有它自己的
这个
参数,这就是调用它的函数。例如,当你说
myfunc.bind(args)
时,
bind
这是
myfunc

通过在
bind
上调用
apply
,我错误地将
bind
的这个赋值给了“mythis”,它不是一个函数,不能对它调用
bind

因此,解决方法是使用

myfunc.bind.apply(myfunc, ["mythis"].concat(["param1", "param2"]))
另外,如果要立即调用绑定的myfunc,可以说:

myfunc.apply.bind(myfunc)("mythis", ["param1", "param2"])
但这还不够,因为我需要将绑定函数作为参数传递给
addEventListener


谢谢你们的帮助,伙计们

您应该将函数用作
apply
方法的第一个参数。使用
myfunc.bind
不会将函数与调用相关联,它具有
function.prototype.bind
的效果,您也可以使用它

bind
方法(
thisArg
)的第一个参数应该是数组中的第一项

var bindedMyfunc = Function.prototype.bind.apply(myfunc, ["mythis", "param1", "param2"]);

也许您想
bind
apply
而不是
apply
ing
bind

var bindedMyfunc = Function.prototype.apply.bind(myfunc);
bindedMyfunc('obj', [1, 2]); // this = obj, arg1 = 1, arg2 = 2
我经常使用此模式使
hasOwnProperty
检查更短而不可隐藏

var has = Function.prototype.call.bind(Object.hasOwnProperty);
has({foo:1}, 'foo'); // true
has({foo:1}, 'bar'); // false
似乎bind本身有它自己的这个参数,这个参数是函数,它被调用。例如,当你说
myfunc.bind(args)
时,
bind
这是
myfunc

没错。如果要应用
bind
,则必须将其应用于函数(第一个参数),并将
bind
参数(包括预期的
值)作为数组(第二个参数)传递:

但是,还有另一种解决问题的方法:将
apply
绑定到函数,并部分应用建议的
apply
参数:

(Function.prototype.apply).bind(myfunc, "mythis", ["param1", "param2"])
// which is equivalent to
(…args) => myfunc.apply("mythis", ["param1", "param2"], …args) // ES6 syntax

啊,我想我明白了,多亏了你的例子,哪里错了
bind
有自己的
this
,在这种情况下,它应该是调用它的函数(
myfunc
)。因此,我必须将
myfunc
作为第一个参数,然后使用
concat
mythis
[param1,param2]
构建一个参数列表。第一个解决方案是
myfunc.bind.apply(myfunc,[“mythis”,“param1”,“param2”)
,您忘记了摘要中的数组文字。第二个方法的问题是
。args
变得无用,我也在考虑类似的内容
(fn.call).bind.apply(call,[fn,“this”,params]).apply.bind()
但是在所有
之后,这个
值变得非常复杂,编写包装器会更干净。是的,
…args
变得毫无用处,但似乎您无论如何都不想在
bindedMufunc()中使用它们呼叫?如果希望
bindedMufunc
接收更多参数,则必须使用第一种方法。@PaulS。抱歉,伙计们,昨天我的问题编辑带来了一些混乱-刚刚成功测试了
myfunc.bind.apply(myfunc,[“mythis”].concat([“param1”,“param2”))
solution。它工作正常。谢谢你的帮助@鲍勃:我想你应该接受古法的回答
(Function.prototype.apply).bind(myfunc, "mythis", ["param1", "param2"])
// which is equivalent to
(…args) => myfunc.apply("mythis", ["param1", "param2"], …args) // ES6 syntax