Skip to main content

在 Electron 中使用 V8 和 Chromium 功能

· 4 min read

构建一个 Electron 应用意味着你只需要创建一个代码库并为一个浏览器进行设计,这非常方便。但由于 Electron 会随着 Node.jsChromium 的发布而保持更新,你也可以利用它们提供的优秀功能。在某些情况下,这还能消除你在之前的网页应用中可能需要包含的依赖。

🌐 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.


有很多功能,我们会在这里举一些例子,但如果你想了解所有功能,可以关注 Google Chromium 博客Node.js 更新日志。你可以在 electronjs.org/#electron-versions 查看 Electron 使用的 Node.js、Chromium 和 V8 的版本。

🌐 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.

箭头函数

findTime () => {
console.log(new Date())
}

字符串插值

var octocat = 'Mona Lisa';
console.log(`The octocat's name is ${octocat}`);

新目标

Octocat() => {
if (!new.target) throw "Not new";
console.log("New Octocat");
}

// Throws
Octocat();
// Logs
new Octocat();

数组包含

// Returns true
[1, 2].includes(2);

剩余参数

// 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?

通过推特@ElectronJS告诉我们你最喜欢在 V8 或 Chromium 中实现的功能。

🌐 Tweet to us @ElectronJS with your favorite features built into V8 or Chromium.