Created
November 28, 2011 13:14
-
-
Save jed/1400363 to your computer and use it in GitHub Desktop.
an attempt to make the new operator dynamically invokeable
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
// usage: newOperator(constructor, [arg1, arg2, ...]) | |
// example: newOperator(Date, [1995, 11, 24]) | |
// => should be identical to new Date(1995, 11, 24) | |
function newOperator(constructor, args) { | |
var i = args.length + 1 | |
, params = [] | |
, fn | |
while (i--) params[i] = "$" + i | |
params.push("return new $0(" + params.slice(1) + ")") | |
fn = Function.apply(null, params) | |
params = params.concat.apply(constructor, args) | |
return fn.apply(null, params) | |
} |
the minus tilde does not give us much more than +1, 2 readable chars with no necessary int cast.
I agree the creation of an empty Array per each call without arguments is way too redundant and also bigger than this solution:
d[c=b?b.length+1:1]
I have edited and as result 2 chars less and faster "no arguments" execution ;-)
doesn't leaving out the number cast mean that the length property could be used to inject code? i'm thinking where args
is {length: "throw 'neener neener'"}
.
well, the initial trick does not solve a thing here ... if you want to screw up functions that would work even with native arrays methods ...
uh .. wait, I know what you mean ... so yes, minus tilde then, updated :D
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in that case, why define the array at all if
args
has no length? can we use this?[EDIT] so something like this: