碎片时间学编程「358]:创建一个函数,获取索引 n 处的参数

创建一个函数,获取索引 n 处的参数。 使用 Array.prototype.slice() 方法获取索引 n 处所需的参数。 如果 n 为负数,则返回倒数第 n 个参数。
const nthArg = n => (...args) => args.slice(n)[0];
示例:
const third = nthArg(2);third(1, 2, 3); // 3third(1, 2); // undefinedconst last = nthArg(-1);last(1, 2, 3, 4, 5); // 5
更多内容请访问我的网站:https://www.icoderoad.com

