Zern
Zern

Reputation: 3

var in function's parameter not working

I added a var in the parameter in the function:

//
msg = "test message";
date = "02-28-2013";
cal.setData({ date : msg});
//
//this can't work

However, the following one works well

//
msg = "test message";
cal.setData({ "02-28-2013" : msg});
//

but I need to use the one containing var. How can I make it work?

thanks

Upvotes: 0

Views: 175

Answers (1)

Sirko
Sirko

Reputation: 74046

Try this:

var param = {}
param[ date ] = msg;
cal.setData( param );

In order to set a variable as the property name for an object, you have to use the bracket notation. To do so, first create an empty object, then add the dynamic property like seen above.

In your first example, the object, that is passed to the function, contains a property named date and not a property with the name of the value of the variable date.

Upvotes: 2

Related Questions