How to unshift or add to the beginning of arguments object in JavaScript

后端 未结 1 741
忘掉有多难
忘掉有多难 2021-01-18 00:42

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 op

相关标签:
1条回答
  • 2021-01-18 01:06
    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.

    0 讨论(0)
提交回复
热议问题