Created
February 8, 2016 10:21
-
-
Save hruan/f002c145072692f5a496 to your computer and use it in GitHub Desktop.
JavaScript WTF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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