Skip to content

Instantly share code, notes, and snippets.

@Chunlin-Li
Created May 10, 2016 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chunlin-Li/a3d993febd5109b871c98472c3249e4e to your computer and use it in GitHub Desktop.
Save Chunlin-Li/a3d993febd5109b871c98472c3249e4e to your computer and use it in GitHub Desktop.
node js arguments

Node js 中通过 arguments 对象修改/增加参数的方法

function foo() {
    console.log(arguments);
    console.log(arguments['extra']);
}

foo.apply({}, {'0':'arg_1', '1':'arg_2', length: 2});
// { '0': 'arg_1', '1': 'arg_2' }
// undefined

foo.apply({}, {'0':'arg_1', '5':'arg_2', length: 2});
// { '0': 'arg_1', '1': undefined }
// undefined

foo.apply({}, {'0':'arg_1', 'extra':'arg_2', length: 2});
// { '0': 'arg_1', '1': undefined }
// undefined

foo.apply({}, {'0':'arg_1', '1':'arg_2', length: 1});
// { '0': 'arg_1' }
// undefined
  • 无法在 arguments 中传递自己命名的属性. 无法通过 apply(this, argumenst) 传递到被调用函数中.
  • key 必须是数字
  • length 属性需要和与最大的 key 值相对应, 否则, 长于 length 的参数无法传递
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment