Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A javascript design pattern for options with default values?

// opt_options is optional function foo(a, b, opt_options) {   // opt_c, opt_d, and opt_e are read from 'opt_options', only c and d have defaults   var opt_c = 'default_for_c';   var opt_d = 'default_for_d';   var opt_e; // e has no default    if (opt_options) {     opt_c = opt_options.c || opt_c;     opt_d = opt_options.d || opt_d;     opt_e = opt_options.e;   } } 

The above seems awfully verbose. What's a better way to handle argument options with default parameters?

like image 356
ripper234 Avatar asked Mar 07 '12 13:03

ripper234


People also ask

What are JavaScript design patterns?

Design patterns are reusable solutions to commonly occurring problems in software design. They are proven solutions, easily reusable and expressive. They lower the size of your codebase, prevent future refactoring, and make your code easier to understand by other developers.

What are default values in JavaScript?

In JavaScript, a parameter has a default value of undefined. It means that if you don't pass the arguments into the function, its parameters will have the default values of undefined .

What is the default type in JavaScript?

Anyways, the default value of a variable in JavaScript is null or undefined .

What are JavaScript options objects?

An options object is an object passed into a method (usually a method that builds a jQuery widget, or similar) which provides configuration info.


2 Answers

This uses jQuery.extend but could be interchanged with an object merger from your library of choice or Object.assign in ES6.

function Module(options){     var defaults = {         color: 'red'     };     var actual = $.extend({}, defaults, options || {});     console.info( actual.color ); }  var a = new Module(); // Red var b = new Module( { color: 'blue' } ); // Blue 

Edit: Now also in underscore or lodash!

function Module(options){     var actual = _.defaults(options || {}, {          color: 'red'     });     console.info( actual.color ); }  var a = new Module(); // Red var b = new Module( { color: 'blue' } ); // Blue 

In Javascript ES6 you can use Object.assign:

function Module(options = {}){     let defaults = {         color: 'red'     };     let actual = Object.assign({}, defaults, options);     console.info( actual.color ); } 
like image 52
max Avatar answered Oct 15 '22 13:10

max


Using the ES2018 spread syntax for object properties:

const defaults = { a: 1, b: 2 };  const ƒ = (given = {}) => {   const options = { ...defaults, ...given };   console.log(options); }; 

Using ES6/ES2015 features, several more options are available.

Using destructuring assignment:

const { a = 1, b = 2 } = options; 

You can also use destructuring function parameters:

const ƒ = ({a = 1, b = 2, c = 3} = {}) => {    console.log({ a, b, c }); }; 

Using Object.assign:

options = Object.assign({}, defaults, options); 

No dependencies!

like image 42
wprl Avatar answered Oct 15 '22 12:10

wprl