Skip to content

Instantly share code, notes, and snippets.

@hruan
Created February 8, 2016 10:21
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 hruan/f002c145072692f5a496 to your computer and use it in GitHub Desktop.
Save hruan/f002c145072692f5a496 to your computer and use it in GitHub Desktop.
JavaScript WTF
function getArgs() {
return arguments;
}
// Node REPL
> const args = getArgs(1, 2, 3)
undefined
> args
{ '0': 1, '1': 2, '2': 3 }
> const argsLiteral = { '0': 1, '1': 2, '2': 3 }
undefined
> Array.prototype.slice.call(args)
[ 1, 2, 3 ]
> Array.prototype.slice.call(argsLiteral)
[]
// Err ... why?
> Object.keys(args)
[ '0', '1', '2' ]
> Object.keys(argsLiteral)
[ '0', '1', '2' ]
> Object.is(args, argsLiteral)
false
> Object.keys(args).every(function(key, i) { return args[key] === argsLiteral[key]; });
true
// WAT?
> Object.getOwnPropertyNames(args)
[ '0', '1', '2', 'length', 'callee' ]
> Object.getOwnPropertyNames(argsLiteral)
[ '0', '1', '2' ]
// Oh ... let's try to fix it
> argsLiteral.length = Object.keys(argsLiteral).length
3
> Array.prototype.slice.call(args)
[ 1, 2, 3 ]
> Array.prototype.slice.call(argsLiteral)
[ 1, 2, 3 ]
// But ...
> Object.keys(args)
[ '0', '1', '2' ]
> Object.keys(argsLiteral)
[ '0', '1', '2', 'length' ]
> Object.is(args, argsLiteral)
false
> Object.keys(args).every(function(key, i) { return args[key] === argsLiteral[key]; });
true
> Object.keys(argsLiteral).every(function(key, i) { return args[key] === argsLiteral[key]; });
true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment