I'd like to be able to pass a key1/value1 pair into a function and have
that function have a local variable by the name of key1 to which value1
is assigned.
for example, I'd like to call
function sample(oOptions ) {
var key1 = "key1 default";
var key2 = "key2 default";
var idx;
if (arguments.leng th>0)
for (idx in oOptions)
window.sample[idx] = oOptions[idx]; // this line wrong
alert (key1);
for (idx in window.sample)
alert(idx + "\n" + window.sample[idx]);
}
with sample({key1:"k ey1 was set"}) and have the alert be:
"key1 was set"
But the above is incorrect since it sets key1 on the function
definition (window.sample) and not the instance being invoked.
How can I fix this up without eval?
Thanks,
Csaba Gabor from Vienna
that function have a local variable by the name of key1 to which value1
is assigned.
for example, I'd like to call
function sample(oOptions ) {
var key1 = "key1 default";
var key2 = "key2 default";
var idx;
if (arguments.leng th>0)
for (idx in oOptions)
window.sample[idx] = oOptions[idx]; // this line wrong
alert (key1);
for (idx in window.sample)
alert(idx + "\n" + window.sample[idx]);
}
with sample({key1:"k ey1 was set"}) and have the alert be:
"key1 was set"
But the above is incorrect since it sets key1 on the function
definition (window.sample) and not the instance being invoked.
How can I fix this up without eval?
Thanks,
Csaba Gabor from Vienna
Comment