vendredi 29 juillet 2016

Merge default with user defined settings

I'm trying to create a plugin with settings. There will be default settings, and user defined settings. I will have to somehow merge the two in an object. I tried the following:

JSFiddle

function MyPlugin(options) {
  if (typeof optoins === null || typeof options !== 'object') options = {};

  var defaults = {
      prop1: true,
      prop2: false,
      prop3: 0,
      prop4: 100,
    }
    // Set default options
  for (var name in defaults) {
    !(name in options) && (this.options[name] = defaults[name]);
  }
}
var test = new MyPlugin();

But I get an error saying:

Uncaught TypeError: Cannot set property 'prop1' of undefined

What's the correct, most efficient way to merge the default and user defined settings?

Aucun commentaire:

Enregistrer un commentaire