How to unshift or add to the beginning of arguments object in JavaScript(如何在 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.
推荐答案
使用
.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 中取消移位或添加到参数对象的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 JavaScript 中取消移位或添加到参数对象的开头


- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01