Warm tip: This article is reproduced from serverfault.com, please click

Can I use colons as variable names in Javascript?

发布于 2013-09-16 14:39:43

Is there a way I can use variables with colons in them as variable name? I would need this for facebook where I have to pass it on as parameters:

FB.api('me/namespace:action', 'POST',
{
  og:type : type,
  og:title : title,
  fb:explicitly_shared : true
});

for example. How would I do this here? When I tried it didn't quite work out..

Questioner
Boehmi
Viewed
0
nnnnnn 2013-09-16 22:41:42

Try this:

FB.api('me/namespace:action', 'POST',
{
  "og:type" : type,
  "og:title" : title,
  "fb:explicitly_shared" : true
});

Note that these aren't "variables", they're object property names. Property names can be just about any valid JS string if you put them in quotes.

For more information see MDN's article Working With Objects.