在 Electron 中使用 V8 和 Chromium 功能
构建 Electron 应用意味着你只需创建一份代码库并针对一个浏览器进行设计,这非常方便。但由于 Electron 会与 Node.js 和 Chromium 保持同步更新,因此你也可以使用它们自带的强大功能。在某些情况下,这可以消除你之前可能需要在 Web 应用中包含的依赖。
¥Building an Electron application means you only need to create one codebase and design for one browser, which is pretty handy. But because Electron stays up to date with Node.js and Chromium as they release, you also get to make use of the great features they ship with. In some cases this eliminates dependencies you might have previously needed to include in a web app.
Electron 项目有很多功能,我们将在这里作为示例介绍其中的一些,但如果你想了解所有功能,可以关注 Google Chromium 博客 和 Node.js 更新日志。你可以查看 electronjs.org/#electron-versions 使用的 Node.js、Chromium 和 V8 Electron 版本。
¥There are many features and we'll cover some here as examples, but if you're interested in learning about all features you can keep an eye on the Google Chromium blog and Node.js changelogs. You can see what versions of Node.js, Chromium and V8 Electron is using at electronjs.org/#electron-versions.
通过 V8 支持 ES6
¥ES6 Support through V8
Electron 将 Chromium 的渲染库与 Node.js 相结合。它们共享同一个 JavaScript 引擎 V8。许多 ECMAScript 2015 (ES6) 功能已内置于 V8 中,这意味着你可以在 Electron 应用中使用它们,而无需任何编译器。
¥Electron combines Chromium's rendering library with Node.js. The two share the same JavaScript engine, V8. Many ECMAScript 2015 (ES6) features are already built into V8 which means you can use them in your Electron application without any compilers.
以下是一些示例,但你也可以获得类(在严格模式下)、块作用域、Promise、类型数组等等。查看 这个清单 以了解更多关于 V8 中 ES6 特性的信息。
¥Below are a few examples but you can also get classes (in strict mode), block scoping, promises, typed arrays and more. Check out this list for more information on ES6 features in V8.
箭头函数
¥Arrow Functions
findTime () => {
console.log(new Date())
}
字符串插值
¥String Interpolation
var octocat = 'Mona Lisa';
console.log(`The octocat's name is ${octocat}`);
新目标
¥New Target
Octocat() => {
if (!new.target) throw "Not new";
console.log("New Octocat");
}
// Throws
Octocat();
// Logs
new Octocat();
数组包含
¥Array Includes
// Returns true
[1, 2].includes(2);
剩余参数
¥Rest Parameters
// Represent indefinite number of arguments as an array
(o, c, ...args) => {
console.log(args.length);
};
Chromium 功能
¥Chromium Features
感谢 Google 和贡献者为 Chromium 付出的辛勤劳动,在你构建 Electron 应用时,你还可以使用以下一些很酷的功能(但不限于):
¥Thanks to all the hard work Google and contributors put into Chromium, when you build Electron apps you can also use cool things like (but not limited to):
关注 Google Chromium 博客,了解新版本发布的功能,同样,你也可以查看 Electron 使用的 Chromium 版本 此处。
¥Follow along with the Google Chromium blog to learn about features as new versions ship and again, you can check the version of Chromium that Electron uses here.
你对什么感到兴奋?
¥What are you excited about?
在 Twitter 上向我们 @ElectronJS 推荐你最喜欢的 V8 或 Chromium 内置功能。
¥Tweet to us @ElectronJS with your favorite features built into V8 or Chromium.