为什么 shift() 在使用 apply() 动态调用的函数中对我的参数工作不正常?
我正在研究动态验证系统。由于某种原因,传递给 validateNumber 的 args 数组 mainfunc 上的 shift() 无法正常运行。这是警告框旁边的注释中输出的代码:
function mainfunc (func){
//this calls the function validateNumber and passes args to it.
this[func].apply(this, Array.prototype.slice.call(arguments, 1));
}
function validateNumber(args) {
alert(args); //this functions normally. for example, displays fish,3,5
var text = args.shift; //would expect this to return 'fish', right?
alert(text); //instead of 'fish' alerts 'function shift() { [native code] }'. This is the problem.
var minimum = args.shift;
var maximum = args.shift;
return text;
}
validationArgs = classList[i].split('-');
functionName = validationArgs.shift();
validationArgs.unshift($(this).val());
mainfunc(functionName, validationArgs); //calls mainfunc which calls the function
我很困惑为什么会这样。注意:我从这个 StackOverflow 答案中抄袭了 mainfunc:调用动态Javascript 中带有动态参数的函数
编辑:哦,天哪。我是个白痴。我什至在问题标题中正确使用了 shift() !谢谢大家。
I'm working on a dynamic validation system. For some reason, shift() on the args array mainfunc passes to validateNumber does not act properly. Here's the code with output in comments next to alert boxes:
function mainfunc (func){
//this calls the function validateNumber and passes args to it.
this[func].apply(this, Array.prototype.slice.call(arguments, 1));
}
function validateNumber(args) {
alert(args); //this functions normally. for example, displays fish,3,5
var text = args.shift; //would expect this to return 'fish', right?
alert(text); //instead of 'fish' alerts 'function shift() { [native code] }'. This is the problem.
var minimum = args.shift;
var maximum = args.shift;
return text;
}
validationArgs = classList[i].split('-');
functionName = validationArgs.shift();
validationArgs.unshift($(this).val());
mainfunc(functionName, validationArgs); //calls mainfunc which calls the function
I'm stumped as to why this behaves this way. Note: I cribbed mainfunc from this StackOverflow answer: Calling dynamic function with dynamic parameters in Javascript
Edit: Oh, my goodness. I am an idiot. I even use shift() correctly in the title of the question! Thanks all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这:
不是方法调用,您想要:
您原来所做的所有操作是将
shift
方法从args
分配给text
,因此您的:输出在您的
警报
中。执行args.shift
不会对args
产生任何影响,因此这两个:只需为您提供另外两个对
args.shift
的引用,而无需更改根本没有args
。This:
is not a method call, you want:
All your original does is assigns the
shift
method fromargs
totext
, hence your:output in your
alert
. Doing aargs.shift
won't have any effect onargs
so these two:just give you two more reference's to
args.shift
without changingargs
at all.错误的。您需要括号来调用函数。否则,您将只获得函数本身(您在警报消息中看到)。
Wrong. You need the parens to call a function. Otherwise you will just get the function itself (which you saw in the alert message).
shift 是一个方法,您是要调用它还是分配引用?
shift is a method, did you mean to call it or assign a reference?
只是为了完成这里发生的事情的解释:
是方法分配。您已将在对象
args
上找到的函数shift
分配给变量p
,但实际上并未执行该函数。此时,p
包含对函数 shift 的引用。因此,当您alert(p)
时,您会看到本机代码引用(即 shift 函数背后的代码)。而:
在
args
对象上找到名为shift
的函数并在不带任何参数的情况下执行它,然后将该方法的返回值分配给变量p
。Just to complete the explanation here of what was going on:
is a method assignment. You've assigned the function
shift
found on the objectargs
to the variablep
, but not actually executed that function. At this point,p
contains a reference to the function shift. Thus, when youalert(p)
, you see a native code reference (that's the code behind the shift function).Whereas:
finds the function named
shift
on theargs
object and executes it without any parameters and then assigns the return value of that method to the variablep
.