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'){
}
});
});
}
$.extendto merge user defined options with your defaults. You normally wouldn't have$(document).readywithin your plugin codeifon final object properties$.extend