这pipeline operator
是已经讨论了很长时间但从未成为 JavaScript 语言的特性之一。这改变了并pipeline operator
作为草案进入了 TC39 流程的第 1 阶段。2021 年,它从第 1 阶段移至第 2 阶段。
目前只有
@babel/plugin-proposal-pipeline-operator · Babel
实现了 生成环境勿用 只做了解
// Piping example:
// Define some functions:
const add = (num) => num1 + 10
const subtract = (num) => num1 - 5
const multiply = (num) => num1 * 9
// Use piping to pass value through cascade of functions:
const num = multiply(add(subtract(15)))
console.log(num)
// Output:
// 180
有了管道运算符 就可以这么写
// Without pipeline operator:
const add = (num1, num2) => num1 + 10
const subtract = (num1, num2) => num1 - 5
const multiply = (num1, num2) => num1 * 9
const num = multiply(add(subtract(15)))
// Log the value of "num":
console.log(num)
// Output:
// 180
// With pipeline operator:
const numPiped = 15 |> add |> subtract |> multiply
// Log the value of "num":
console.log(numPiped)