I'm trying to change function arguments in javascript.
f = function(){
console.log(a,b,c);
};
SetArgumentList( f, ["a", "b", "c"] );
f(1,2,3);
// should print "1 2 3"
// [edit]
// alternatively SetArgumentList could also work like
f = SetArgumentList( f, ["a", "b", "c"] );
Is there some solid way of doing this?
Where do I need it?... basically I'm trying to add type checked functions:
Object.prototype.method = function( name, typedef, func ){ ... }
function Thing(){};
Thing.method("log",
{ arr: Array, str: String },
function(){
console.log(arr, str);
});
t = new Thing();
t.log([1,2,3], "ok");
t.log("err", "ok"); // <-- causes an exception
// I know I can do this way
Thing.method("log",
[Array, String],
function(arr, str){
console.log(arr, str);
});
// but that's harder to read
NOTE! I know how to do type checking, but not the new function construction.
a,bandcare bound to, as you cannot change what scope they are bound to. But the only scope you can inject into like that is the global scope, oh well. Inlining lexically would be compatible with lexical scoping in theory (you'd define a new function = re-compute bindings) but it's impossible. It would also be very fragile (remember C preprocessor macros?). Long story short, it's far easier to just play by the rules.