开发者

Using a variable as part of a javascript object property

Another very simple question.

I have some code like this:

var a = {"foo":"bar"};

Which creates an object called a with a property called foo with value bar.

I'd like to do this:

var propName = "foo";
a = {propName:"bar"};

And for th开发者_高级运维e two a objects to be the same.

How do I do that?

(Context is that I'm building an object to pass to jQuery's css method)


You must set it in two steps:

var a = {};
a[propName] = 'bar';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜