Skip to main content

JavaScript 环境

JavaScript 运行时

¥JavaScript Runtime

使用 React Native 时,你将在最多三种环境中运行 JavaScript 代码:

¥When using React Native, you're going to be running your JavaScript code in up to three environments:

  • 在大多数情况下,React Native 将使用 Hermes,一个针对 React Native 优化的开源 JavaScript 引擎。

    ¥In most cases, React Native will use Hermes, an open-source JavaScript engine optimized for React Native.

  • 如果 Hermes 被禁用,React Native 将使用 JavaScript 核心,即为 Safari 提供支持的 JavaScript 引擎。请注意,在 iOS 上,由于 iOS 应用中缺乏可写的可执行内存,JavaScriptCore 不使用 JIT。

    ¥If Hermes is disabled, React Native will use JavaScriptCore, the JavaScript engine that powers Safari. Note that on iOS, JavaScriptCore does not use JIT due to the absence of writable executable memory in iOS apps.

  • 使用 Chrome 调试时,所有 JavaScript 代码都在 Chrome 本身内运行,通过 WebSocket 与原生代码进行通信。Chrome 使用 V8 作为其 JavaScript 引擎。

    ¥When using Chrome debugging, all JavaScript code runs within Chrome itself, communicating with native code via WebSockets. Chrome uses V8 as its JavaScript engine.

虽然这些环境非常相似,但你最终可能会遇到一些不一致的情况。最好避免依赖任何运行时的细节。

¥While these environments are very similar, you may end up hitting some inconsistencies. It is best to avoid relying on specifics of any runtime.

JavaScript 语法转换器

¥JavaScript Syntax Transformers

语法转换器允许你使用新的 JavaScript 语法,而无需等待所有解释器的支持,从而使编写代码变得更加愉快。

¥Syntax transformers make writing code more enjoyable by allowing you to use new JavaScript syntax without having to wait for support on all interpreters.

React Native 随 Babel JavaScript 编译器 一起提供。检查 Babel 文档 其支持的转换以获取更多详细信息。

¥React Native ships with the Babel JavaScript compiler. Check Babel documentation on its supported transformations for more details.

React Native 启用的转换的完整列表可以在 @react-native/babel-preset 中找到。

¥A full list of React Native's enabled transformations can be found in @react-native/babel-preset.

](https://ts.nodejs.cn/"/>)

转型代码
ECMAScript 5
保留字
promise.catch(function() {...});
ECMAScript 2015 (ES6)
箭头函数
<C onPress={() => this.setState({pressed:true})} />

聚合物填料

¥Polyfills

许多标准函数也可在所有受支持的 JavaScript 运行时上使用。

¥Many standard functions are also available on all the supported JavaScript runtimes.

浏览器

¥Browser

ECMAScript 2015 (ES6)

ECMAScript 2016 (ES7)

ECMAScript 2017 (ES8)

具体的

¥Specific

  • __DEV__