typescript 函数名末尾的 $ 符号表示什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43083577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:23:43  来源:igfitidea点击:

What does $ sign at the end of function name indicate?

javascripttypescriptecmascript-6rxjs

提问by ishandutta2007

Here is the code I'm reviewing...

这是我正在的代码...

import { Observable } from 'rxjs/Rx';
// reducer
import { playerRegister, PlayerState } from './player';
export function getPlayer$ (state$: Observable<MyAppState>): Observable<PlayerState> {
  return state$.select(state => state.player);
};

回答by Robby Cornelissen

Syntactically, the dollar ($) character has no special meaning in JavaScript identifiers.

从语法上讲,美元 ( $) 字符在JavaScript 标识符中没有特殊含义。

It is, however, sometimes used by convention to indicate that a variable holds an Observableor that a function will return an Observable.

然而,有时按照惯例使用它来表示一个变量持有一个Observable或一个函数将返回一个Observable

回答by Matt Burnell

I'm not sure if it's used more widely than within the RxJS community, but within this community it's commonly used to indicate that a variable is a stream (i.e. an Observable) or that a function returns such a stream.

我不确定它是否比在 RxJS 社区中的使用更广泛,但在这个社区中,它通常用于表示变量是一个流(即一个 Observable)或者一个函数返回这样一个流。

回答by ggradnig

This is a code convention named Finnish Notation, apparently due to the origin of the developerthat is attributedfor first using it. It's used to indicate the Observabletype of a variable or function.

这是一个名为Finnish Notation的代码约定,显然是由于首先使用它的开发人员的出身。它用于指示变量或函数的类型。Observable

The idea is that an Observable usually represents a stream of multiple Valuesand a pluralized variable / function name would indicate this. To not be confused with array variables (which are usually also pluralized), the $character is used instead of the s. When reading the variable, you'd read the $as an s.

这个想法是一个 Observable 通常代表一个多个 Value流,一个复数变量/函数名称将表明这一点。为了不要与数组变量(通常也是复数形式)混淆,使用$字符而不是s. 读取变量时,您会将 读取$s.

Example

例子

When naming an array, you'll most likely use the proper plural form of a single element's name, as in:

命名数组时,您很可能会使用单个元素名称的正确复数形式,例如:

const pets = ['cat', 'dog', 'turtle']

const pets = ['cat', 'dog', 'turtle']

While, if you had an observable that emitted those three values, you'd use:

同时,如果您有一个发出这三个值的 observable,您可以使用:

const pet$ = from(['cat', 'dog', 'turtle']) // read: pets

const pet$ = from(['cat', 'dog', 'turtle']) // read: pets

It's up to you and your team whether you want to use it. I guess there is no explicit consensus as of now, so you can have a long and meaningful argument about it ;-). There are already tslint rulesavailable that allow you to enforce your decision.

是否要使用它取决于您和您的团队。我想到目前为止还没有明确的共识,因此您可以对此进行长期而有意义的争论;-)。已经有可用的tslint 规则允许您执行您的决定。