开发者

Using a variable as part of a javascript object property

开发者 https://www.devze.com 2023-02-06 01:18 出处:网络
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.

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

精彩评论

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