From d94b4f47476bbc901753f68d6dfe317e1c5c732e Mon Sep 17 00:00:00 2001 From: MDN Date: Sat, 29 May 2021 01:14:43 +0000 Subject: [CRON] sync translated content --- .../operators/pipeline_operator/index.html | 76 ++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 files/zh-cn/orphaned/web/javascript/reference/operators/pipeline_operator/index.html (limited to 'files/zh-cn/orphaned') diff --git a/files/zh-cn/orphaned/web/javascript/reference/operators/pipeline_operator/index.html b/files/zh-cn/orphaned/web/javascript/reference/operators/pipeline_operator/index.html new file mode 100644 index 0000000000..12c3b6fbb3 --- /dev/null +++ b/files/zh-cn/orphaned/web/javascript/reference/operators/pipeline_operator/index.html @@ -0,0 +1,76 @@ +--- +title: 管道操作符 +slug: orphaned/Web/JavaScript/Reference/Operators/Pipeline_operator +tags: + - Experimental + - JavaScript + - Operator + - 语法糖 + - 链式 + - 链式调用 +translation_of: Web/JavaScript/Reference/Operators/Pipeline_operator +original_slug: Web/JavaScript/Reference/Operators/Pipeline_operator +--- +
{{jsSidebar("Operators")}} {{SeeCompatTable}}
+ +

试验性的管道操作符 |> (目前其标准化流程处于 stage 1 阶段)允许以一种易读的方式去对函数链式调用。本质上来说,管道操作符是单参数函数调用的语法糖,它允许你像这样执行一个调用:

+ +
let url = "%21" |> decodeURI;
+ +

使用传统语法写的话,等效的代码是这样的:

+ +
let url = decodeURI("%21");
+
+ +

语法

+ +
expression |> function
+ +

例子

+ +

函数链式调用

+ +

当链式调用多个函数时,使用管道操作符可以改善代码的可读性。

+ +
const double = (n) => n * 2;
+const increment = (n) => n + 1;
+
+// 没有用管道操作符
+double(increment(double(5))); // 22
+
+// 用上管道操作符之后
+5 |> double |> increment |> double; // 22
+
+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
Pipeline operator draftStage 1Not part of the ECMAScript specification yet.
+ +

浏览器兼容性Edit

+ +
+ + +

{{Compat("javascript.operators.pipeline")}}

+
+ +

参见

+ + -- cgit v1.2.3-54-g00ecf