Would like to check the parameters passed to a function and replace not specified ones or the ones which "I dont like". For that, I've done the following (The reason why I want to use arguments is that I want to have a generic function doing that) (same results with node.js, just change Logger to console):
function argtest (x,y,z) {
Logger.log ("at begin x="+x+", y="+y+", z="+z);
arguments.length=3; // found this somewhere, doesn't help
arguments[0]="a";
arguments[1]="b";
arguments[2]="c";
Logger.log ("arguments now:");
for (var i in arguments) {
Logger.log ("arguments["+i+"]="+arguments[i]);
}
Logger.log ("at end x="+x+", y="+y+", z="+z);
}
function callArgTest () {
argtest ();
argtest (1,2,3);
}
So the result of calling callArgTest is:
Logger.log([at begin x=undefined, y=undefined, z=undefined, []])
Logger.log([arguments now:, []])
Logger.log([arguments[0]=a, []])
Logger.log([arguments[1]=b, []])
Logger.log([arguments[2]=c, []])
Logger.log([at end x=undefined, y=undefined, z=undefined, []])
Logger.log([at begin x=1, y=2, z=3, []])
Logger.log([arguments now:, []])
Logger.log([arguments[0]=a, []])
Logger.log([arguments[1]=b, []])
Logger.log([arguments[2]=c, []])
Logger.log([at end x=a, y=b, z=c, []])
So the parameters x,y,z only change, when they were initially specified. In the first case (calling argtest without parameters) they remain undefined even after assigning values to arguments. So what is missing here to make it work? Note that I dont want to touch x,y,z. Instead in the final implementation I want to pass the arguments to a function which works on them.
The issue is that, when calling argtest(), arguments doesn't actually have a "0" property to refer to x, or "1" for y, etc.:
argtest() => arguments = { "length": 0 }
argtest(1,2,3) => arguments = { "length": 3, "0": x, "1": y, "2": z }
The definition of that reference is described in step 11.c of ES5 10.6, which defines getters and setters for each "index" of arguments, but is limited by both the number of arguments named and the number of arguments passed.
Regarding your intent:
I want to throw in into an existing implementation at the beginning of functions a call to a function which simply takes the arguments and some defaults as parameter, e.g.
defaults (arguments, "string=nana", "number=4")
You'll probably find something like this easier:
function argTest(x, y, z) {
x = typeof x !== 'undefined' ? x : ...;
y = typeof y !== 'undefined' ? y : ...;
// etc.
}
What you want to do is possible with ES5, but would mean defining your own "arguments" object with getters and setters for each "index" and named argument, whether passed a value or not, via Object.create/Object.defineProperties or get/set -- and, I wouldn't consider this "simply":
function argtest(x, y, z) {
defaults({
length: argtest.length,
get '0'() { return x; },
set '0'(val) { x = val; },
get '1'() { return y; },
set '1'(val) { y = val; },
get '2'() { return z; },
set '2'(val) { z = val; }
}, ...);
// ...
}
After
arguments[0]="a";
arguments[1]="b";
arguments[2]="c";
why don't you add
x = arguments[0];
y = arguments[1];
z = arguments[2];
? I imagine there's some reason why this doesn't meet your goals, but I guess I need a more realistic example in order to see what really would meet your goals.
Perhaps you could do something like this:
var args = Array.prototype.slice.call(arguments);
And then work with args? You could pass args into other functions are you need. Let me know if this helps you out, or if I'm barking up the wrong tree.