简体   繁体   English

JavaScriptCore和React Native上的ES6

[英]ES6 on JavaScriptCore and React Native

I'm trying to figure out what parts of ES6 are supported on React Native. 我试图弄清楚React Native是否支持ES6的哪些部分。 I'm aware of the Babel transformations that are listed here , but I'm having a hard time figuring out what JavaScriptCore supports on its own without any transpiling. 我知道这里列出的Babel转换,但是我很难弄清楚JavaScriptCore自己支持什么而没有任何转译。 Like will JavaScriptCore support different things on different platforms and devices? 就像JavaScriptCore在不同的平台和设备上支持不同的东西一样吗? Is it different on iOS 8 vs iOS9? iOS 8和iOS9有所不同吗?

For instance I wrote some code that uses generator functions and ES6 collection types, and those are not part of what's listed in the Babel transformations, and it works fine in React Native on the devices where I tested. 例如,我编写了一些使用生成器函数和ES6集合类型的代码,这些代码不属于Babel转换中列出的内容,并且在我测试的设备上的React Native中运行良好。 But how do I know whether it will work for all devices I'm targeting? 但是我怎么知道它是否适用于我定位的所有设备?

As you mentioned React Native is using JavaScriptCore as Javascript engine. 正如您提到的,React Native将JavaScriptCore用作Javascript引擎。 JavaScriptCore is also the engine used by iOS' Safari. JavaScriptCore也是iOS的Safari使用的引擎。 For Andriod in general V8 is used. 对于Andriod,通常使用V8。 So you would only have to look the current feature support up in a compatability table like this one. 因此,您只需要在这样的兼容性表中查找当前功能支持

The JavaScriptCore version does depend on your iOS version, so for example on iOS 8 you don't have access to some ES6 methods, as explained here: https://mcculloughwebservices.com/2016/11/29/adding-support-es2015-number-methods/ JavaScriptCore版本确实取决于您的iOS版本,因此例如,在iOS 8上,您无权访问某些ES6方法,如此处所述: https : //mcculloughwebservices.com/2016/11/29/adding-support-es2015 -number-methods /

They even made a polyfill following their post, so just install it: 他们甚至在帖子发布后制作了一个polyfill ,因此只需安装即可:

npm install --save react-native-polyfill

And load it at the beginning of your app: 并将其加载到您的应用程序的开头:

import 'react-native-polyfill'

I ended up here because Number.isInteger just raised an error on the device of someone using my React Native app: 我到这里结束了,因为Number.isInteger刚刚在使用我的React Native应用程序的某人的设备上引发了一个错误:

TypeError: undefined is not a function (evaluating 'Number.isInteger(u)') TypeError:undefined不是函数(评估'Number.isInteger(u)')

They have iOS 8 .1.2. 他们有iOS 8 .1.2。 Their userAgent is Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B440 他们的userAgent是Mozilla / 5.0(iPhone; CPU iPhone OS 8_1_2,例如Mac OS X) AppleWebKit / 600.1.4 (KHTML,例如Gecko)移动设备/ 12B440

However on iOS 10.3.1 (using my iPhone or the simulator), this method exists and works fine. 但是,在iOS 10.3.1(使用我的iPhone或模拟器)上,此方法存在并且可以正常工作。 My userAgent is Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Mobile/14E8301 我的userAgent是Mozilla / 5.0(iPhone; CPU iPhone OS 10_3_1,例如Mac OS X) AppleWebKit / 603.1.30 (KHTML,例如Gecko)移动设备/ 14E8301

Besides, according to Caniuse , this feature appeared in Safari Mobile 9. 此外,据Caniuse称 ,此功能出现在Safari Mobile 9中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM