words

Javascript function defaults with assoc arrayJavascript function default paraméter asszociatív tömb használatával

I wrote an object method to ease the processing of functions default properties when you using associative arrays.

Object.prototype.combine = function(_args){
  for(var i in this) {
    if(typeof _args[i] == "undefined") {
      _args[i] = this[i];
    }
  }
};

  function feedTheCat(args){
    var defaults = {
        'morning' : "nothing",
        'noon'    : "pork",
        'nite'    : "mouse"
    };
    defaults.combine(args);

    console.log = args.morning+" | "+args.noon+" | "+args.nite;
  }

feedTheCat({'morning': "milk", 'nite': "kitekat"}); // -> milk | pork | kitekat

Inspiration

Object.prototype.combine = function(_args){
  for(var i in this) {
    if(typeof _args[i] == "undefined") {
      _args[i] = this[i];
    }
  }
};

  function feedTheCat(args){
    var defaults = {
        'morning' : "nothing",
        'noon'    : "pork",
        'nite'    : "mouse"
    };
    defaults.combine(args);

    console.log = args.morning+" | "+args.noon+" | "+args.nite;
  }

feedTheCat({'morning': "milk", 'nite': "kitekat"}); // -> milk | pork | kitekat

Last updated: