24

I am using node.js v4.3.1

I would like to use named parameters in calling functions as they are more readable.

In python, I can call a function in this manner;

info(spacing=15, width=46)

How do I do the same in node.js?

My javascript function looks something like this;

function info(spacing, width)
{
   //implementation
{
guagay_wk
  • 26,337
  • 54
  • 186
  • 295
  • 2
    Javascript does not have named function parameters. Passing an object literal (`{ param:value }`) is usually how it is done in Javascript. – Sverri M. Olsen Feb 28 '16 at 08:53
  • 1
    Possible duplicate of [Named parameters in javascript](http://stackoverflow.com/questions/11796093/named-parameters-in-javascript) – Anderson Green Nov 23 '16 at 23:43

2 Answers2

22

The standard Javascript way is to pass an "options" object like

info({spacing:15, width:46});

used in the code with

function info(options) {
    var spacing = options.spacing || 0;
    var width = options.width || "50%";
    ...
}

as missing keys in objects return undefined that is "falsy".

Note that passing values that are "falsy" can be problematic with this kind of code... so if this is needed you have to write more sophisticated code like

var width = options.hasOwnProperty("width") ? options.width : "50%";

or

var width = "width" in options ? options.width : "50%";

depending on if you want to support inherited options or not.

Pay also attention that every "standard" object in Javascript inherits a constructor property, so don't name an option that way.

6502
  • 112,025
  • 15
  • 165
  • 265
  • Why do you add those `||` stuff? Are they default values? Am I right? – guagay_wk Feb 28 '16 at 08:35
  • 1
    @user1824987: yes... if for example `width` is not passed `options.width` will return `undefined` and therefore the `||` operator will pick the right side instead. – 6502 Feb 28 '16 at 08:38
  • Is it safer to always use `options.hasOwnProperty`? what do you mean by "falsy"? – guagay_wk Feb 28 '16 at 08:45
  • 1
    @user1824987 It works much like [null-coalescing operators](https://en.wikipedia.org/wiki/Null_coalescing_operator) in other languages. If the stuff before the `||` evaluates to `false` then the part on the right will be used instead. – Sverri M. Olsen Feb 28 '16 at 08:57
19

It is easier with ES6. nodejs > 6.5 supports these features.

You should check out this link:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

The exact usage you want to use is implemented. However I would not recommend it.

The code below (taken from the link above) is a better practice because you don't have to remember in which order you should write the parameters.

function drawES6Chart({size = 'big', cords = { x: 0, y: 0 }, radius = 25} = {}) {
console.log(size, cords, radius);
 // do some chart drawing
}

you can use this function by doing:

const cords = { x: 5, y: 30 }
drawES6Chart({ size: 'small', cords: cords })

This way functions get more understandable and it gets even better if you have variables named size, cords and radius. Then you can do this using object shorthand.

// define vars here
drawES6Chart({ cords, size, radius })

order does not matter.

Utku Turunç
  • 334
  • 1
  • 7