I am new to Javascript (and programming in general) and have been searching for a way to change the value of an arbitrary number of aguments using a Javascript function.
The answer here (JavaScript variable number of arguments to function) was quite helpful. I was able to use it to create the two of the functions I need, but I'm having trouble with the third.
Basically I would like to pass a variable number of objects (primitive or more complex) into a function and have the function change the value of each object.
var pants = true;
var house = true;
var hair = {/* lots of stuff */};
var onFire = function() {
for (var i = 0; i < arguments.length; i++) {
arguments[i] = false;
}
};
onFire(pants, house, hair);
Console outputs:
>pants;
true
>house;
true
>hair;
Object
How can I formulate this function so the the result is:
>pants;
false
>house;
false
>hair;
false
Any help would be greatly appreciated!
Edit:
To clarify things - I am trying to create a reusable helper function that changes the value of any object passed in to false
instead of typing:
var a1 = false;
var a2 = false;
...
var a+n = false;
If I use mathec's method - is it possible to 'parse' object
so that it's properties overwrite the global variables of the same name, or am I stuck with typing it out explicitly each time?
var object = {
pants: {/* lots of stuff */},
house: {/* lots of stuff */},
hair: {/* lots of stuff */}
};
function modifyObject(obj) {
obj.pants = false;
obj.house = false;
obj.hair = false;
}
function someMagic(object){
// wand waves...
// rabbit exits hat....
}
Console Output:
>pants;
false
>house;
false
>hair;
false