[英]“callback is not the function” and function.apply()
Why this simply doesn't work? 为什么这根本行不通?
It says typeof(callback) = undefined. 它说typeof(callback)= undefined。
function A(a,callback)
{
document.write(typeof(callback));
callback();
return a;
}
function Run(func,args)
{
return func.apply(this||window, args || [
function () { document.write("blah")}
]);
}
Run(A,[1]);
However, without using function.apply it works properly: 但是,在不使用function.apply的情况下,它可以正常运行:
function Run2(func,arg)
{
return func(arg,
function () { document.write("blah")}
);
}
Run2(A,1);
Please be patient I'm new to JS. 请耐心等待,我是JS新手。
The issue is actually occuring in the A
function with the callback
variable. 该问题实际上是在带有
callback
变量的A
函数中发生的。 If you had written the A
function as below you will not receive this error: 如果您编写了如下的
A
函数,则不会收到此错误:
function A(a, callback) {
var type = typeof callback;
document.write(type);
if(type == "function") callback();//ensure callback is a function
return a;
}
The first argument of apply is the scope, while the second is an array of arguments. apply的第一个参数是作用域,第二个参数是参数数组。 It appears you have this, but
args
with Run(A,[1]);
看来您有这个,但带有
Run(A,[1]);
args
Run(A,[1]);
is only a single argument (1) which will be aligned with the a
parameter, however you're missing the function. 仅是一个与参数
a
对齐的参数(1),但是您缺少该函数。 On the other hand, if args
is not set, you're opting to create an array with a single argument of [ function ()... ]
, which will again will be aligned with a
. 在另一方面,如果
args
没有设置,您加入到用一个参数创建一个数组[ function ()... ]
这将再次将对齐a
。
From the looks of it, you're attempting to merge/concatenate the two arrays, when in fact ||
从外观上来看,你试图合并/串联这两个阵列的时候,其实
||
is used as a comparison operator, or rather an or
assignment. 用作比较运算符,或用作
or
赋值。
Try this: 尝试这个:
func.apply(this||window,args.concat([function () { document.write("blah")}]));
or 要么
args.push(function () { document.write("blah")});
func.apply(this||window,args);
apply()
takes the first argument and uses it as this
in the function call, and uses the second argument as the arguments to that call. apply()
会将第一个参数,并使用它作为this
函数调用,并使用第二个参数作为参数传递给该呼叫。 So Run2 is calling A like this (where <func>
is your anonymous function): 因此Run2像这样调用A(其中
<func>
是您的匿名函数):
A(1, <func>);
Run is only passing the one argument in your argument array, 1
and calling it like this: Run只在参数数组中传递一个参数
1
,然后像这样调用它:
A(1)
What you want to do is replace your Run with this (I think): 您要执行的操作是用以下命令替换“运行”(我认为):
function Run(func,args)
{
args.push(function () { document.write("blah")});
// args is now [1, function(){...}]
return func.apply(this||window, args);
}
You would need an Array length of 2 for your Run()
function to work using A()
, because it takes two arguments. 为了使
Run()
函数使用A()
,您需要2的数组长度,因为它需要两个参数。 You just have 1 in args, as [1]
. 您在args中只有1,如
[1]
。 You want [1, func]
, so the following would be your solution: 您需要
[1, func]
,因此以下是您的解决方案:
// the keyword this always exists
function Run(func, args){
args.push(func);
return func.apply(this, args || [function(){document.write("blah")}]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.