如何不印字或添加参数的对象开始在JavaScript中 [英] How to unshift or add to the beginning of arguments object in JavaScript

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

问题描述

我刚刚学到的<一个href=\"http://stackoverflow.com/questions/19903841/removing-an-argument-from-arguments-in-javascript\">convention对于突然离开的参数的第一个元素阵列(我还了解到实际上是一个对象) 。现在我需要做的是相反的。我需要使用不印字操作的值增加的参数年初阵列(或对象的演技就像一个数组)。这可能吗?我想:

  Array.prototype.unshift.apply('你好',参数);

这对参数没有影响任何责任。


解决方案

  1. 使用 .CALL()而不是。适用()调用的unshift()


  2. 设置参数 )的本值的unshift(


  3. 设置'你好'作为参数传递给的unshift()



  Array.prototype.unshift.call(参数,'你好');


由于@lonesomeday指出的那样,你可以使用。适用()而不是 .CALL(),但你需要传递一个数组类参数作为第二个参数。所以你的情况,你需要换行'你好'在数组中。

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);

That had no effect on arguments whatsoever.

解决方案

  1. use .call() instead of .apply() to invoke unshift()

  2. set arguments as the this value of unshift()

  3. set 'hello' as the argument to unshift()


Array.prototype.unshift.call(arguments, '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天全站免登陆