Javascript unshift arguments return unexpected results

router.route = function (alias, pattern, action, constraints) {
   if (2 === arguments.length)
     Array.prototype.unshift.call(arguments, undefined)

   console.log(arguments)
}

router.route('/artist/:id', function () {})


{ '0': undefined,
  '1': '/artist/:id',
  '2': [Function],
  '3': undefined,
  '4': undefined,
  '5': undefined,
  '6': undefined,
  '7': undefined,
  '8': undefined,
  '9': undefined,
  '10': undefined,
  '11': undefined,
  '12': undefined,
  '13': undefined,
  '14': undefined,
  '15': undefined,
  '16': undefined,
  '17': undefined,
  '18': undefined,
  '19': undefined }

what I'm basically trying to do is to make the alias argument optional, but I'm trying to find a way to do this without doing this.

if (2 === arguments.length) {
  action = pattern
  pattern = alias
  alias = undefined
}

so the unshift () function basically works, I get the same result. alias = undefined pattern = '/artist/:id' action = function () {}

but the problem is that out of nowhere I added 17 "undefined 's" added to the end of the argument array.

it will affect performance and does anyone know why this happened?

change

I accidentally wrote Array.prototype.unshift (arguments, undefined) instead of Array.prototype.unshift.call (arguments, undefined) in my original question, very sorry.

+3
source share
2 answers

undefined - , , . , , .

0

, ?

, :

var args = Array.prototype.shift.call(arguments); 
0

All Articles