这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

试验性的管道操作符 |> (目前其标准化流程处于 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 draft Stage 1 Not part of the ECMAScript specification yet.

浏览器兼容性Edit

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
Pipeline operator (|>)
Experimental
Chrome No support NoEdge No support NoFirefox Full support 58
Disabled
Full support 58
Disabled
Disabled From version 58: this feature is behind the --enable-pipeline-operator compile flag.
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android Full support 58
Disabled
Full support 58
Disabled
Disabled From version 58: this feature is behind the --enable-pipeline-operator compile flag.
Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
User must explicitly enable this feature.
User must explicitly enable this feature.

参见