Reputation: 13
I have a problem with my JavaScript code. I'm starting with some more complex things right now, seemed to find some answers on the net, but unfortunately I can't get it fixed. The problem is:
I want the variables sGetMobileField
and ValMob
to get in the parameters, but like this it isn't working:
var oFieldValues = { sGetMobileField:) { Value: ValMob } };
Variables don't seem to work as a object property. Anybody can help me fix it?
Thanks, Dane
Upvotes: 0
Views: 1881
Reputation: 25332
First of all the syntax doesn't look right. I guess the ")" after sGetMobileField:
is a typo. However, what are you doing here is set a property called "sGetMobileField":
var oFieldValues = { sGetMobileField: { Value: ValMob } };
Exactly for the same reason that with Value
are you set a property called "Value" and not a property that get its name from a Value
variable. It is consistent, right? So you will have:
console.log(oFieldValues.sGetMobileFields.Value) // the content of ValMob.
Luckily in JS you can use the square bracked notation instead of the dot notation. It means, you can access to a property using a string. So, for instance:
console.log("Hello");
is the same of:
console["log"]("Hello");
Therefore, you can use the value of a variable to specify the property of the object to access. In your case:
var oFieldValues = {};
oFieldValues[sGetMobileField] = { Value: ValMob };
Notice that following the naming convention usually used in JS, Value
should be value
and ValMob
should be valMob
.
Upvotes: 0
Reputation: 74096
Try this
var oFieldValues = { };
oFieldValues[ sGetMobileField ] = { Value: ValMob };
You can use variables as property identifiers, but not inside an object literal. You have to create the object first, and may then add dynamic properties using
obj[ varToHoldPropertyName ] = someValue;
Upvotes: 4