为什么我不能在函数的参数上调用数组方法? [英] Why can't I call an array method on a function's arguments?

查看:122
本文介绍了为什么我不能在函数的参数上调用数组方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以接受任意数量参数的函数...

I have a function that can accept any number of arguments...

const getSearchFields = () => {        
    const joined = arguments.join('/'); 
};

我想要传递给与 / 字符。我一直收到这个错误:

I want a string of all the arguments being passed to the function joined with the / character. I keep getting this error:


args.join不是函数

args.join is not a function

有人可以告诉我我做错了吗?

Can someone please tell me what I'm doing wrong?

推荐答案

arguments 是一个伪数组,而不是真实数组。 join 方法适用于数组。

arguments is a pseudo-array, not a real one. The join method is available for arrays.

你需要作弊:

var convertedArray = [];

for(var i = 0; i < arguments.length; ++i)
{
 convertedArray.push(arguments[i]);
}

var argsString = convertedArray.join('/');

与其他帖子类似,您可以做以下简写:

Similar to other posts, you can do the following as shorthand:

var argsString = Array.prototype.join.call(arguments, "/");

这篇关于为什么我不能在函数的参数上调用数组方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆