Skip to main content

7.2.0 发布:私有实例方法

· 6 min read

我们刚刚发布了 Babel 的一个新小版本!

🌐 We just released a new minor version of Babel!

此版本增加了对私有实例方法的支持,并修复了许多与 Flow 和 TypeScript 类型相关的 bug。你可以在 GitHub 上阅读完整的更新日志。

🌐 This release includes support for private instance methods and a bunch of bug fixes regarding Flow and TypeScript types. You can read the whole changelog on GitHub.

在这个版本中,很多新贡献者修复了 bug 或实现了新功能:感谢 Gcaufy、Grigory Moroz、Paul Happ、Tim McClure 和 Veaceslav Cotruta!

🌐 A lot of new contributors fixed bugs or implemented new features in this release: thanks to Gcaufy, Grigory Moroz, Paul Happ, Tim McClure and Veaceslav Cotruta!

向彭博社大声致谢,感谢他们赞助实现私有类元素!对私有 实例 方法的支持是对 Babel 7.1.0 发布的私有 静态 字段的后续。

🌐 A big shout out to Bloomberg for sponsoring the implementation of private class elements! This support for private instance methods is a follow-up to private static fields released in Babel 7.1.0.

如果你或你的公司想支持 Babel 和 JavaScript 的发展,但又不确定如何做,你可以在 OpenCollective 向我们捐款,更棒的是,可以直接与我们合作实现 新的 ECMAScript 提案!

🌐 If you or your company wants to support Babel and the evolution of JavaScript, but aren't sure how, you can donate to us on OpenCollective and, better yet, work with us on the implementation of new ECMAScript proposals directly!

私有实例方法 (#8654)​

🌐 Private Instance Methods (#8654)

JavaScript
class Person {
#age = 19;

#increaseAge() {
this.#age++;
}

birthday() {
this.#increaseAge();
alert("Happy Birthday!");
}
}

感谢 Tim 实现了这个提议,也感谢 Nicolò 和 Justin 的审阅!

🌐 Thanks to Tim for implementing this proposal, and to Nicolò and Justin for the reviews!

你可以通过在 Babel 配置中添加 @babel/plugin-proposal-private-methods 插件,或者在 repl 中启用 stage-3 预设来测试私有方法。

🌐 You can test private methods by adding the @babel/plugin-proposal-private-methods plugin to your Babel configuration, or enabling the stage-3 preset in the repl.

私有访问器也即将到来,而且我们已经做了一些重大的内部重构,这将很快让我们可以在装饰器中支持私有元素 🎉。

🌐 Private accessors are also coming, and we have done some big internal refactoring that allows us to add support for private elements to decorators soon 🎉.

“智能”管道操作符解析 (#8289)​

🌐 "Smart" Pipeline Operator Parsing (#8289)

多亏了 James DiGioia 和 J. S. Choi 的工作,@babel/parser 现在除了可以解析 最小版本 外,也能解析 智能管道操作符。

🌐 Thanks to the work of James DiGioia and J. S. Choi, @babel/parser now also can parse the Smart Pipeline Operator, in addition to the minimal version.

我们目前只支持智能流水线提案的“核心”部分,不支持任何附加功能。我们目前仅支持 # 作为初步占位符。实际的占位符尚未决定,将来 @babel/parser 可能会实验性地支持其他选项,例如 ?、@ 和 %。

🌐 We currently only support the "core" of the smart pipeline proposal, and not any additional features. We also currently support only # as a preliminary placeholder. The actual placeholder has not yet been decided, and other possibilities such as ?, @, and % may be experimentally supported by @babel/parser in the future.

JavaScript
// "Smart"
const result = 2 |> double |> 3 + # |> toStringBase(2, #); // "111"

// "Simple"
const result = 2 |> double |> (x => 3 + x) |> (x => toStringBase(2, x));

Babel 实现了这个提案的多种版本,以帮助 TC39 测试并收集社区反馈。像所有提案一样,未来可能会有变化。

🌐 Babel implements multiple variants of this proposal to help TC39 test and gather feedback from the community. As with all proposals, expect changes in the future.

如果你直接使用 @babel/parser 并且想测试这个提议,你可以将 proposal: "smart" 选项传给管道插件:

🌐 If you are directly using @babel/parser and you want to test this proposal, you can pass the proposal: "smart" option to the pipeline plugin:

JavaScript
const ast = babel.parse(code, {
plugins: [
["pipelineOperator", { proposal: "smart" }]
]
})

我们还不支持转译这种语法,但很快就会支持的。

🌐 We don't support transpiling this syntax yet, but it will come soon.

插件名称 (#8769)​

🌐 Plugin Names (#8769)

现在每个官方插件都会向 Babel 提供它的名字。虽然这不会影响 Babel 的正常使用,但它为每个插件提供了一个统一的标识符。这对于像 Time Travel 这样的功能特别有用,它可以让你准确看到每个插件对你的代码做了什么。你可以通过我们的 repl 看到效果:

🌐 Every official plugin now provides Babel its name. Although this doesn't affect normal Babel use, it provides a consistent identifier for each plugin. This is particularly useful for things like Time Travel, which allows you to see exactly what each plugin is doing to your code. You can see this in effect via our repl:


在Twitter上讨论