Skip to content

Instantly share code, notes, and snippets.

@jed
Created November 28, 2011 13:14
Show Gist options
  • Save jed/1400363 to your computer and use it in GitHub Desktop.
Save jed/1400363 to your computer and use it in GitHub Desktop.
an attempt to make the new operator dynamically invokeable
// 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)
}
@WebReflection
Copy link

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 ...

@WebReflection
Copy link

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