1
\$\begingroup\$

I'm sure there's a better way to approach the following. I'm writing a plugin where users can enter settings in the following format:

Setting: "object1setting -> object2setting"

It's done this way to allow for multiple objects to be passed within the same JS call. I'm breaking the the strings apart at the "->" symbol and saving them, however there are many settings and I'm thinking there has to be a more efficient way of writing this:

// START PLUGIN, SETUP DEFAULT OPTIONS, EXTEND OPTIONS TO "O" .....
// I'VE JUST PULLED SOME RANDOM SETTINGS AS AN EXAMPLE. 

bg_x_speed_in_set = o.bg_x_speed_in.toString().split("->"), // X PROPERTY SPEED IN
bg_x_speed_out_set = o.bg_x_speed_out.toString().split("->"), // X PROPERTY SPEED OUT
bg_y_speed_out_set = o.bg_y_speed_out.toString().split("->"), // Y PROPERTY SPEED OUT

...
// ETC

This continues on and gets lengthy. Everything works perfectly in all browsers. I'm just trying to clean up the code.

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

I think you can use object literals. The user of your plugin can pass in an array of object literals:

var settings = [
    {bg_x_speed_in: ..., bg_x_speed_out_set : ...}
    , {bg_x_speed_in: ..., bg_y_speed_out_set : ....} 
    , {bg_x_speed_in: ..., bg_x_speed_out_set : ....} 
]; 

In your code, loop through the array; each element contains the settings for one object.

\$\endgroup\$
2
  • \$\begingroup\$ Trying to keep the user settings as simple as possible, or I would definitely go this route \$\endgroup\$
    – Aaron
    Commented Feb 17, 2012 at 18:29
  • \$\begingroup\$ This actually seems easier to me than delimited strings. Most reusable js libraries are using object literals to pass in settings. \$\endgroup\$
    – Giscard Biamby
    Commented Feb 17, 2012 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.