Using _ (underscore) variable with arrow functions in ES6/Typescript

Halt

I came across this construct in an Angular example and I wonder why this is chosen:

_ => console.log('Not using any parameters');

I understand that the variable _ means don't care/not used but since it is the only variable is there any reason to prefer the use of _ over:

() => console.log('Not using any parameters');

Surely this can't be about one character less to type. The () syntax conveys the intent better in my opinion and is also more type specific because otherwise I think the first example should have looked like this:

(_: any) => console.log('Not using any parameters');

In case it matters, this was the context where it was used:

submit(query: string): void {
    this.router.navigate(['search'], { queryParams: { query: query } })
      .then(_ => this.search());
}
Estus Flask

The reason why this style can be used (and possibly why it was used here) is that _ is one character shorter than ().

Optional parentheses fall into the same style issue as optional curly brackets. This is a matter of taste and code style for the most part, but verbosity is favoured here because of consistency.

While arrow functions allow a single parameter without parentheses, it is inconsistent with zero, single destructured, single rest and multiple parameters:

let zeroParamFn = () => { ... };
let oneParamFn = param1 => { ... };
let oneParamDestructuredArrFn = ([param1]) => { ... };
let oneParamDestructuredObjFn = ({ param1 }) => { ... };
let twoParamsFn = (param1, param2) => { ... };
let restParamsFn = (...params) => { ... };

Although is declared but never used error was fixed in TypeScript 2.0 for underscored parameters, _ can also trigger unused variable/parameter warning from a linter or IDE. This is a considerable argument against doing this.

_ can be conventionally used for ignored parameters (as the other answer already explained). While this may be considered acceptable, this habit may result in a conflict with _ Underscore/Lodash namespace, also looks confusing when there are multiple ignored parameters. For this reason it is beneficial to have properly named underscored parameters (supported in TS 2.0), also saves time on figuring out function signature and why the parameters are marked as ignored (this defies the purpose of _ parameter as a shortcut):

let fn = (param1, _unusedParam2, param3) => { ... };

For the reasons listed above, I would personally consider _ => { ... } code style a bad tone that should be avoided.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Methods in ES6 objects: using arrow functions

From Dev

Immediate function using JavaScript ES6 arrow functions

From Dev

ES6 arrow functions

From Dev

Typescript overload arrow functions

From Dev

ECMAScript 6 arrow functions

From Java

Is it necessary to use underscore (_) variable inside functions in Dart?

From Dev

Compiling ES6 arrow functions to Es5 using Babel.js

From Dev

Using ES6 Arrow Functions in Node 0.11 w/ Foo.prototype

From Dev

es6 arrow functions debugger statement

From Dev

Are ES6 arrow functions incompatible with Angular?

From Dev

ES6 arrow functions not working on the prototype?

From Dev

javascript es6 double arrow functions

From Dev

Argument types in functions versus in arrow functions in ES6

From Dev

ES6 Assign a variable with an arrow function

From Dev

ES6 Assign a variable with an arrow function

From Dev

Using arrow functions with d3

From Dev

simple grade calculator using arrow functions Javascript

From Dev

Using integer variable as key in underscore template

From Dev

Using integer variable as key in underscore template

From Dev

Arrow Functions and This

From Dev

es6katas.org Kata #6: arrow functions - binding

From Java

What is the syntax for Typescript arrow functions with generics?

From Dev

How to pass arguments in arrow functions in typescript

From Dev

ECMA Script 6 Arrow functions as object properties

From Dev

Affect this in a variable instead of using the fat arrow in coffeescript

From Dev

PHP : Using variable functions

From Java

Is it possible to export Arrow functions in ES6/7?

From Java

When should I use `return` in es6 Arrow Functions?

From Dev

How to run ES6 code with arrow functions in Safari?

Related Related

HotTag

Archive