简体   繁体   中英

Can I use colons as variable names in Javascript?

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..

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM