Search code examples
javascriptjquerynotation

JavaScript/jQuery short hand function definitions


I'm using a jQuery plugin that has its functions defined as such:

    $('#mydiv').pluginAction({
        someproperty: val, 
        format: 'mm hh',
        labels: ['yes', 'no', 'maybe'], 
        labels1: ['never', 'always']
    });

In my HTML page, I have multiple DIVs that have the same properties for format, labels, labels1, but different values for someproperty. Is there some type of JavaScript notation I can take advantage of to shorten the definition so that I don't have to have duplicate code?


Solution

  • There are a couple of ways of dealing with this:

    1. Create a function which fills in the blanks; or

    2. If the plugin is yours, default those values to what you want.

    Example of (1):

    function props(val) {
      return {
        someproperty: val,
        format: 'mm hh',
        labels: ['yes', 'no', 'maybe'], 
        labels1: ['never', 'always']
      };
    }
    
    $("#mydiv").pluginAction(props("..."));