简体   繁体   中英

JavaScript: How to build key-value pairs

In jQuery you can pass key-value pairs like this

$(this).colorbox({ width:100, height:500 });

I'm wondering if there is a way to build such key-value pair, eg. something like this

var pairs = ???;

if(someCondition)
  pairs.Add('width', '100');
else
  pairs.Add('maxWidth', '200');

$(this).colorbox(pairs);
var obj = {};
obj[key] = value;

You create an object, then assign your values under your keys.

There's no prototyped method to do this, but you'd probably just use regular property access capabilities...

if(someCondition)
  pair.width = '100';
else
  pair.maxWidth = '200';

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