I know you can not set a key value dynamically, but what about the value?
I’m asking b.c. I need to know the best way to initialize an object. Here is how I do it currently:
var object_pipe = {
model : 'MUserAny',
page : {
email: '0'
},
args : {
foo : '0'
}
};
object_pipe.args.foo = localStorage.foo; // Statement A
I’d like to put statement A directly in the object literal but am not sure if it is possible or the correct syntax.
Aslo, Do I need to explicitly declare properties when I can not set them in the object literal as in here…does page need to be there?
var object_pipe = {
model : 'MUserNew',
page : {
}
};
for( var key in form_elements ) {
object_pipe.page[ form_elements[key].name ] = form_elements[key].value ;
}
You don’t need to explicitly declare variables but you can just initialize the key with an empty “whatever”, can be an array, string, object literal etc. You can add properties later.