Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript反移位参数返回意外结果_Javascript_Node.js - Fatal编程技术网

javascript反移位参数返回意外结果

javascript反移位参数返回意外结果,javascript,node.js,Javascript,Node.js,我基本上是想让“alias”参数成为可选的 但我正试图找到一种不用这样做的方法 router.route = function (alias, pattern, action, constraints) { if (2 === arguments.length) Array.prototype.unshift.call(arguments, undefined) console.log(arguments) } router.route('/artist/:id', f

我基本上是想让“alias”参数成为可选的 但我正试图找到一种不用这样做的方法

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 }
所以unshift()基本上是有效的,我得到了相同的结果。
alias=未定义
pattern='/artist/:id'
action=function(){}

但问题是,不知从哪里我在arguments数组的末尾添加了17个“undefined”

这会影响性能吗?有人知道为什么会发生这种情况吗

编辑
我在最初的问题中无意中编写了Array.prototype.unshift(参数,未定义)而不是Array.prototype.unshift.call(参数,未定义),非常抱歉。

如果由于资源超时而存在这些未定义的位置,那么是的,性能将受到影响。如果它只是一个空数组索引,则不会影响性能。

如果我理解正确,您不想删除数组的第一个元素吗

如果是这样,您只需执行以下操作:

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

我认为OP想要相反的结果。
alias
参数是可选的,因此,如果只传递了3个参数,则假定未传递
alias
,因此需要将传递的值移到其正确的参数中,使
alias
保持未定义状态。我将查看console.log(参数)在取消移位之前显示。这将在对arguments对象应用取消移位时发生。[]取消移位(参数“字符串”)将导致相同的行为。
var args = Array.prototype.shift.call(arguments);