Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created February 22, 2010 18:34
Show Gist options
  • Select an option

  • Save subtleGradient/311328 to your computer and use it in GitHub Desktop.

Select an option

Save subtleGradient/311328 to your computer and use it in GitHub Desktop.
Rough proof-of-concept on how to get an array of arguments with accessing `arguments`. Needs benchmarking to verify that it actually improves performance; Could be optimized
/*
---
source : http://gist.github.com/gists/311328
provides : getArg
description : Get an array of arguments with accessing `arguments`
version : Rough proof-of-concept
author : Thomas Aylott
site : SubtleGradient.com
copyright : 2010 Thomas Aylott
license : MIT
...
*/
function getArg(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,lastArgument){
var args = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,lastArgument],
lastArgumentIndex,
undefined;
if (lastArgument === undefined){
// lastArgument is undefined? trim the trailing undefined arguments
lastArgumentIndex = args.length-1;
while (lastArgumentIndex > 0 && lastArgument === undefined)
lastArgument = args[--lastArgumentIndex];
args.splice(lastArgumentIndex+1);
} else {
// Too many arguments?! Use `arguments` :'(
args = Array.prototype.slice.call(arguments);
lastArgumentIndex = args.length-1;
lastArgument = args[lastArgumentIndex];
}
// console.log('Expected', arguments.length-1, String(arguments[arguments.length-1]));
// console.log('Actual ', lastArgumentIndex, String(lastArgument));
// console.log( args.length, Array.prototype.slice.call(arguments).length);
// console.log( args, Array.prototype.slice.call(arguments));
// console.assert( args.length === Array.prototype.slice.call(arguments).length, "Bad length! :'(");
return args;
}
getArg(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,"Last Arg")
getArg(1,2,3,4,5,6,7,8,9,0,"Last Arg")
getArg(1,2,3,4,null,6,7,8,9,0,"Last Arg")
getArg(1,2,3,4,undefined,6,7,8,9,0,"Last Arg")
getArg(1,2,3,4,void+0,6,7,8,9,0,"Last Arg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment