0

I am writing a jQuery plugin. There is an object of options that I want to loop through to set regular variables with. It would basically set a variable that would be the index of the object item. I want to determine if any options are left blank and if they are, set the regular variable to the default value. I would usually use the following to set options to a default if they are blank:

 var defaults = {
     someVar1 : "somevar1",
      omeVar2:  "somevar2"
 };

var someVar1;
var someVar2;

function init(options, defaults){
   if(typeof options.someVar1 === 'undefined'){
      someVar1 = defaults.someVar1;
   } else {
      someVar1 = options.someVar1;
  }
   return something();
}
function something(){
   console.log(item);
});

This can be a big pain in the butt if I have a lot of options to set. How could I modify my code below to dynamically define global variables?

function init(element, options){
    $(document).ready(function(){
         $.each(options, function(index, value){
             if(typeof options.index === 'undefined'){

              }
         });
     });
 }
7
  • just use $.extend to merge user defined options with your defaults. You normally wouldn't have $(document).ready within your plugin code Commented Oct 19, 2014 at 2:33
  • Yeah I actually do use $.extend I just didn't think it was relevant enough to include it here. Commented Oct 25, 2014 at 20:24
  • why do you need all these individual variables then? After you merge user opts with defaults just use if on final object properties Commented Oct 25, 2014 at 20:38
  • Doing it that way will make the code much cleaner rather than having to use an if for every action I preform. Commented Oct 25, 2014 at 20:42
  • whatever works for you, doesn't make much sense without seeing more of the plugin code. Normally you would be testing after the $.extend Commented Oct 25, 2014 at 20:52

2 Answers 2

1

Lets say you have an object :

var my_object = {
    item1 : 1,
    item2 : 2,
    item3 : 3,
}

var i;
for(i in my_object){
    console.log(i); 
    console.log(my_object[i]);
}
/*
    This will print in the console:
    item1
    1
    item2
    2
    item3
    3
*/

for each will not find an undefined, because undefined does not exist, unless your object is like this:

var my_object = {
    item1 : 1,
    item2 : 2,
    item3 : 3,
    item4 : undefined
}

Also :

$.each(options, function(index, value){
  // if(typeof options.index === 'undefined'){ <-- this is wrong
     if(typeof options[index]=== 'undefined'){ <-- this is correct
        // Also you have the value, why not "value === undefined" ?
     }
});
Sign up to request clarification or add additional context in comments.

4 Comments

I guess I could use the .each on my defaults object instead of options. PS: I didn't think to include that object when I posted my example :/
And so how would I use the index to define a variable?
One more thing... why is options.index wrong? Wouldn't you use that since you're working with an object?
index is a variable. When you say object.index it's like saying object['index'], the index here is not a variable its the key.
0

It sounds like you might want to setup a predefined variable array then based on your index of the option read its default value

var optionDefaults = ["val1", "val2", "val3"];
function init(element, options){
    $(document).ready(function(){
         $.each(options, function(index, value){
             if(typeof options.index === 'undefined'){
                  var optiondefValue = optionDefaults[index];
                  //do whatever you want here.
              }
         });
     });
 }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.