Skip to content

Instantly share code, notes, and snippets.

@Incognito
Created September 12, 2012 14:53
Show Gist options
  • Select an option

  • Save Incognito/3707161 to your computer and use it in GitHub Desktop.

Select an option

Save Incognito/3707161 to your computer and use it in GitHub Desktop.
Forwarding arguments of variadic function calls.
function q(){
console.log(arguments.length + " from q");
var b = function(){
console.log(arguments.length + " from b");
}
b.apply(this, arguments);
}
q(); //0 from q, 0 from b
q(1); //1 from q, 1 from b
q(1, 2); //2 from q, 2 from b
q(1, 2, 3, 1); //4 from q, 4 from b
@mkmcdonald
Copy link
Copy Markdown

You are missing a semicolon in the variable declaration. Also, you should move that declaration to the top (i.e. var b;) to reduce confusion.

@mkmcdonald
Copy link
Copy Markdown

Also, your use of this will point to the global object in non-strict mode code and undefined in strict mode code if new is omitted. I would just pass null for thisArg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment