在 ES6/Typescript 中使用带有箭头函数的 _(下划线)变量 [英] Using _ (underscore) variable with arrow functions in ES6/Typescript

查看:88
本文介绍了在 ES6/Typescript 中使用带有箭头函数的 _(下划线)变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个 Angular 示例中遇到了这个构造,我想知道为什么选择它:

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());
}

推荐答案

可以使用这种样式的原因(也可能是这里使用它的原因)是 _ 比 <短一个字符代码>().

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) => { ... };

虽然 已声明但从未使用过 错误 在 TypeScript 2.0 中修复了下划线参数,_ 也可以触发 未使用的变量/参数 来自 linter 或 IDE 的警告.这是反对这样做的一个相当大的论据.

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.

_ 通常可以用于忽略的参数(正如其他答案已经解释过的).虽然这可能被认为是可以接受的,但这种习惯可能会导致与 _ Underscore/Lodash 命名空间的冲突,当有多个忽略的参数时也会看起来很混乱.出于这个原因,正确命名下划线参数是有益的(在 TS 2.0 中支持),还可以节省找出函数签名以及为什么参数被标记为忽​​略的时间(这违背了 _ 参数的目的作为快捷方式):

_ 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.

这篇关于在 ES6/Typescript 中使用带有箭头函数的 _(下划线)变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆