为什么 shift() 在使用 apply() 动态调用的函数中对我的参数工作不正常?

发布于 2024-11-28 16:23:51 字数 1137 浏览 0 评论 0原文

我正在研究动态验证系统。由于某种原因,传递给 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千仐 2024-12-05 16:23:51

这:

var text = args.shift;

不是方法调用,您想要:

var text = args.shift();

您原来所做的所有操作是将 shift 方法从 args 分配给 text,因此您的:

function shift() { [native code] }

输出在您的警报中。执行 args.shift 不会对 args 产生任何影响,因此这两个:

var minimum = args.shift;
var maximum = args.shift;

只需为您提供另外两个对 args.shift 的引用,而无需更改根本没有args

This:

var text = args.shift;

is not a method call, you want:

var text = args.shift();

All your original does is assigns the shift method from args to text, hence your:

function shift() { [native code] }

output in your alert. Doing a args.shift won't have any effect on args so these two:

var minimum = args.shift;
var maximum = args.shift;

just give you two more reference's to args.shift without changing args at all.

如痴如狂 2024-12-05 16:23:51
var text = args.shift; //would expect this to return 'fish', right?

错误的。您需要括号来调用函数。否则,您将只获得函数本身(您在警报消息中看到)。

var text = args.shift();
var text = args.shift; //would expect this to return 'fish', right?

Wrong. You need the parens to call a function. Otherwise you will just get the function itself (which you saw in the alert message).

var text = args.shift();
微暖i 2024-12-05 16:23:51
var text = args.shift; //would expect this to return 'fish', right?

shift 是一个方法,您是要调用它还是分配引用?

var text = args.shift; //would expect this to return 'fish', right?

shift is a method, did you mean to call it or assign a reference?

孤蝉 2024-12-05 16:23:51

只是为了完成这里发生的事情的解释:

var p = args.shift;

是方法分配。您已将在对象 args 上找到的函数 shift 分配给变量 p,但实际上并未执行该函数。此时,p 包含对函数 shift 的引用。因此,当您 alert(p) 时,您会看到本机代码引用(即 shift 函数背后的代码)。

而:

var p = args.shift();

args 对象上找到名为 shift 的函数并在不带任何参数的情况下执行它,然后将该方法的返回值分配给变量 p

Just to complete the explanation here of what was going on:

var p = args.shift;

is a method assignment. You've assigned the function shift found on the object args to the variable p, but not actually executed that function. At this point, p contains a reference to the function shift. Thus, when you alert(p), you see a native code reference (that's the code behind the shift function).

Whereas:

var p = args.shift();

finds the function named shift on the args object and executes it without any parameters and then assigns the return value of that method to the variable p.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文