0

Is the following definition of defaults bad practice?

var defaults = {
    name:'',
    surname:'',
    address:{
        part1:'',
        part2:'',
        postCode:'' 
    }
};

If only part1 in the address object is defined by the plugin initiator the rest of address will be undefined.

What would be the best way to handle this? Or should I not use complex objects as options at all?

Matthew Grima
  • 1,513
  • 5
  • 25
  • 40
  • Is this going to be global? Then it is bad practice... – Sidharth Mudgal Oct 01 '12 at 21:04
  • What do you mean "the rest of address will be undefined"? If you don't need them, why define them? – Sidharth Mudgal Oct 01 '12 at 21:08
  • Let's say I want to use them in a string variable down the line like so: var address = part1 + part2 + postCode; And postCode is not defined. That would not just add the empty but 'undefined'. I could check when using them in the future but I'm asking whether I just just not create defaults in that format. – Matthew Grima Oct 01 '12 at 21:11
  • Note the above is just an example and not my actual defaults. But it is outlines my situation. – Matthew Grima Oct 01 '12 at 21:12

1 Answers1

1

Ok, so it was just a case of wording in my searches before I asked the question.

I didn't know that jQuery.extend() can extend recursively by using

this.opts = $.extend(true,{}, defaults, options); 

Instead of

this.opts = $.extend({}, defaults, options);
Matthew Grima
  • 1,513
  • 5
  • 25
  • 40