如何在 JavaScript 中取消移位或添加到参数对象的开头 [英] How to unshift or add to the beginning of arguments object in JavaScript

查看:21
本文介绍了如何在 JavaScript 中取消移位或添加到参数对象的开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚学习了 弹出第一个元素的约定arguments 数组(我也学到了其实是一个Object).现在我需要做相反的事情.我需要使用 unshift 操作将值添加到 arguments 数组(或 Object 像数组一样)的开头.这可能吗?我试过了:

I've just learned the convention for popping off the first element of the arguments array (which I also learned is actually an Object). Now I need to do the opposite. I need to use an unshift operation to add a value to the beginning of the arguments array (or Object acting like an array). Is this possible? I tried:

Array.prototype.unshift.apply('hello', arguments);

这对 arguments 没有任何影响.

That had no effect on arguments whatsoever.

推荐答案

  1. 使用 .call() 而不是 .apply() 来调用 unshift()

arguments 设置为 unshift()

'hello' 设置为 unshift()

<小时>

Array.prototype.unshift.call(arguments, 'hello');

<小时>

正如@lonesomeday 所指出的,您可以使用 .apply() 而不是 .call(),但您需要传递一个类似数组的参数作为第二个争论.因此,在您的情况下,您需要将 'hello' 包装在一个数组中.


As @lonesomeday pointed out, you can use .apply() instead of .call(), but you need to pass an array-like argument as the second argument. So in your case, you'd need to wrap 'hello' in an Array.

这篇关于如何在 JavaScript 中取消移位或添加到参数对象的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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