了解React Native中的不同JavaScript環境

by Khoa Pham

通過Khoa Pham

了解React Native中的不同JavaScript環境 (Get to know different JavaScript environments in React Native)

React Native can be very easy to get started with, and then at some point problems occur and we need to dive deep into it.

React Native可能很容易上手 ,然后在某些時候出現問題,我們需要深入研究它。

The other day we had a strange bug that was only occurring in production build, and in iOS only. A long backtrace in the app revealed that it was due to Date constructor failure.

前幾天,我們遇到了一個奇怪的錯誤,該錯誤僅在生產版本中發生,并且僅在iOS中發生。 應用程序中的長時間回溯顯示這是由于Date構造函數故障引起的。

const date = new Date("2019-01-18 12:00:00")

This returns the correct Date object in debug mode, but yields Invalid Date in release. What’s special about Date constructor? Here I’m using react native 0.57.5 and no Date libraries.

這會在調試模式下返回正確的Date對象,但在發行版中會產生Invalid DateDate構造函數有何特別之處? 在這里,我使用的是react native 0.57.5,并且沒有Date庫。

日期構造器 (Date constructor)

The best resource for learning Javascript is via Mozilla web docs, and entering Date:

學習Javascript的最佳資源是通過Mozilla Web文檔,然后輸入Date :

Creates a JavaScript Date instance that represents a single moment in time. Date objects use a Unix Time Stamp, an integer value that is the number of milliseconds since 1 January 1970 UTC.

創建一個表示單個時刻JavaScript Date實例。 Date對象使用Unix時間戳記 ,它是一個整數值,它是自1970年1月1日UTC以來的毫秒數。

Pay attention to how Date can be constructed by dateString:

注意如何用dateString構造Date:

dateStringString value representing a date. The string should be in a format recognized by the Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601).

dateString表示日期的字符串值。 該字符串應采用Date.parse()方法可識別的格式( 符合IETF的RFC 2822時間戳以及ISO8601的版本 )。

So Date constructor uses static method Date.parse under the hood. This has very specific requirement about the format of date string that it supports

所以Date構造Date.parseDate.parse使用靜態方法Date.parse 。 這對它支持的日期字符串的格式有非常具體的要求

The standard string representation of a date time string is a simplification of the ISO 8601 calendar date extended format (see Date Time String Format section in the ECMAScript specification for more details). For example, "2011-10-10" (date-only form), "2011-10-10T14:48:00" (date-time form), or "2011-10-10T14:48:00.000+09:00" (date-time form with milliseconds and time zone) can be passed and will be parsed. When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as local time.

日期時間字符串的標準字符串表示形式是ISO 8601日歷日期擴展格式的簡化形式(有關更多詳細信息,請參見ECMAScript規范中的“ 日期時間字符串格式”部分)。 例如, "2011-10-10" (僅日期格式), "2011-10-10T14:48:00" (日期時間格式)或"2011-10-10T14:48:00.000+09:00" (具有毫秒和時區的日期時間格式)可以傳遞并進行解析。 缺少時區偏移時,僅日期格式將解釋為UTC時間,而日期時間格式將解釋為本地時間。

The ECMAScript specification states: If the String does not conform to the standard format the function may fall back to any implementation–specific heuristics or implementation–specific parsing algorithm. Unrecognizable strings or dates containing illegal element values in ISO formatted strings shall cause Date.parse() to return NaN.

ECMAScript規范指出:如果String不符合標準格式,則該函數可能會退回到任何特定于實現的試探法或特定于實現的解析算法。 ISO格式的字符串中無法識別的字符串或包含非法元素值的日期將導致Date.parse()返回NaN

The reason that we get Invalid Date in iOS must be because the code was run in two different JavaScript environments and they somehow have different implementation of the Date parsing function.

我們在iOS中獲得無效日期的原因一定是因為代碼是在兩個不同JavaScript環境中運行的,并且它們在某種程度上對日期解析功能具有不同的實現。

JavaScript環境 (JavaScript Environment)

React Native guide has a dedicated section about JavaScript environments.

React Native指南專門介紹了JavaScript環境 。

When using React Native, you’re going to be running your JavaScript code in two environments:

使用React Native時,您將在兩種環境中運行JavaScript代碼:

  • In most cases, 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.

    在大多數情況下,React Native將使用JavaScriptCore (支持SafariJavaScript引擎)。 請注意,在iOS上,由于iOS應用程序中沒有可寫的可執行內存,因此JavaScriptCore不使用JIT。

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

    使用Chrome調試時,所有JavaScript代碼都在Chrome本身中運行,并通過WebSockets與本機代碼進行通信。 Chrome使用V8作為其JavaScript引擎。

While both environments are very similar, you may end up hitting some inconsistencies. We’re likely going to experiment with other JavaScript engines in the future, so it’s best to avoid relying on specifics of any runtime.

盡管兩種環境非常相似,但您最終可能會遇到一些不一致之處。 將來我們可能會嘗試使用其他JavaScript引擎,因此最好避免依賴任何運行時的細節。

React Native also uses Babel and some polyfills to have some nice syntax transformers, so some of the code that we write may not be necessarily supported natively by JavascriptCore.

React Native還使用Babel和某些polyfill來具有一些不錯的語法轉換器,因此JavascriptCore不一定必須支持我們編寫的某些代碼。

Now it is clear that while we debug our app via Chrome debugger, it works because V8 engine handles that. Now try turning off Remote JS Debugging: we can see that the above Date constructor fails, which means it is using JavascriptCore.

現在很明顯,當我們通過Chrome調試器調試應用程序時,它可以工作,因為V8引擎可以處理它。 現在嘗試關閉遠程JS調試:我們可以看到上面的Date構造函數失敗,這意味著它正在使用JavascriptCore

To confirm this issue, let’s run our app in Xcode and go to the Safari app on MacOS to enter Development menu. Select the active Simulator and choose JSContext on the current iOS app. Remember to turn off Remote JS Debugging so that the app uses JavascriptCore:

為確認此問題,讓我們在Xcode中運行我們的應用程序,然后轉到MacOS上的Safari應用程序以進入“開發”菜單。 選擇活動的Simulator,然后在當前iOS應用上選擇JSContext。 請記住關閉遠程JS調試,以便該應用程序使用JavascriptCore:

Now open the Console in Safari dev tools, and we should have access to JavascriptCore inside our app. Try running the above Date constructor to confirm that it fails:

現在,在Safari開發工具中打開控制臺,我們應該可以在應用程序內訪問JavascriptCore。 嘗試運行上面的Date構造函數以確認它失敗:

有效日期字符串格式是什么? (What are legit date string formats?)

Since 2016, JavascriptCore supports most ES6 features:

自2016年以來, JavascriptCore支持大多數ES6功能:

As of r202125, JavaScriptCore supports all of the new features in the ECMAScript 6 (ES6) language specification

從r202125開始 ,JavaScriptCore支持ECMAScript 6(ES6)語言規范中的所有新功能。

And it was fully confirmed a year later in JSC ? ES6

一年后,它在JSC中得到了充分證實? ES6

ES2015 (also known as ES6), the version of the JavaScript specification ratified in 2015, is a huge improvement to the language’s expressive power thanks to features like classes, for-of, destructuring, spread, tail calls, and much more

ES2015 (也稱為ES6),在2015年批準了JavaScript規范的版本,是一個巨大的進步了語言的表達能力歸功于像功能類 , 換的 , 解構 , 傳播 , 尾調用和更

WebKit’s JavaScript implementation, called JSC (JavaScriptCore), implements all of ES6

WebKitJavaScript實現(稱為JSC(JavaScriptCore)) 實現了所有ES6

For more details about JavaScript features supported by different JavaScript engines, visit this ECMAScript comparison table.

有關不同JavaScript引擎支持JavaScript功能的更多詳細信息,請訪問此ECMAScript比較表 。

Now for the date string format, from Date.parse, let’s visit ECMAScript 2015 specification to see what it says about date string format:

現在,從Date.parse中獲取日期字符串格式,讓我們訪問ECMAScript 2015規范,以了解日期字符串格式的含義:

ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ

ECMAScript基于ISO 8601擴展格式的簡化,為日期時間定義了字符串交換格式。 格式如下: YYYY-MM-DDTHH:mm:ss.sss Z

Where the fields are as follows:

字段如下:

"T" appears literally in the string, to indicate the beginning of the time element.

"T"字面上出現在字符串中,以指示時間元素的開始。

So JavascriptCore requires T specifier and V8 can work without it. The fix for now is to always include that T specifier. This way we always follow ECMAScript standards to make sure it works across different JavaScript environments.

因此, JavascriptCore需要T說明符,而V8可以不使用它。 現在的解決方法是始終包含該T指定符。 這樣,我們始終遵循ECMAScript標準,以確保其可在不同JavaScript環境中正常工作。

const date = new Date("2019-01-18 12:00:00".replace(' ', 'T'))

And now it returns correct Date object. There may be difference between JavascriptCore on iOS and macOS, and among different iOS versions. The lesson learned here is that we should always test our app thoroughly in production and on devices to make sure it works as expected.

現在,它返回正確的Date對象。 iOS和macOS上的JavascriptCore之間以及不同的iOS版本之間可能存在差異。 從這里吸取的教訓是,我們應該始終在生產環境和設備上全面測試我們的應用程序,以確保其能夠按預期運行。

翻譯自: https://www.freecodecamp.org/news/get-to-know-different-javascript-environments-in-react-native-4951c15d61f5/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392199.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392199.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392199.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

分布與并行計算—生命游戲(Java)

生命游戲其實是一個零玩家游戲,它包括一個二維矩形世界,這個世界中的每個方格居住著一個活著的或死了的細胞。一個細胞在下一個時刻生死取決于相鄰八個方格中活著的或死了的細胞的數量。如果相鄰方格活著的細胞數量過多,這個細胞會因為資源匱…

正確認識 Vista 激活期限

當我們在安裝 Vista 時,可以不輸入序列號進行安裝,這和以往的操作系統安裝有所不同,我們不必再為安裝系統時找不到我們的序列號標簽而發愁。如果不輸入序列號而繼續安裝系統,那么系統將提示我們有30天的激活期限!這里的…

Oracle使用hs odbc連接mssql2008

1.創建odbc 2.在 product\11.2.0\dbhome_1\hs\admin\ 下拷貝initdg4odbc,把名字改為initcrmsql(init所建odbc的名稱) HS_FDS_CONNECT_INFO crmsql #odbc名稱 HS_FDS_TRACE_LEVEL 0 HS_FDS_RECOVERY_ACCOUNTsa #要連接的數據庫名稱 HS_FDS_RECOVERY_PWD…

oracle修改物化視圖字段,獲取物化視圖字段的修改矢量(一)

當表建立了物化視圖日志之后,表的DML修改會被記錄到物化視圖日志中,而物化視圖日志則包含了一個修改矢量,來記錄哪個列被修改。在文章列的修改矢量可以通過2的N次方來獲得,也就是POWER(2, N)。而N的值,就是列的位置。但…

聚合 數據處理_R中聚合的簡介:強大的數據處理工具

聚合 數據處理by Satyam Singh Chauhan薩蒂揚辛格喬漢(Satyam Singh Chauhan) R中聚合的簡介:強大的數據處理工具 (An introduction to aggregates in R: a powerful tool for playing with data) Data Visualization is not just about colors and graphs. It’s …

大數據 notebook_Dockerless Notebook:數據科學期待已久的未來

大數據 notebookData science is hard. Data scientists spend hours figuring out how to install that Python package on their laptops. Data scientists read many pages of Google search results to connect to that database. Data scientists write a detailed docume…

【NGN學習筆記】6 代理(Proxy)和背靠背用戶代理(B2BUA)

1. 什么是Proxy模式? 按照RFC3261中的定義,Proxy服務器是一個中間的實體,它本身即作為客戶端也作為服務端,為其他客戶端提供請求的轉發服務。一個Proxy服務器首先提供的是路由服務,也就是說保證請求被發到更加”靠近”…

分布與并行計算—并行計算π(Java)

并行計算π public class pithread extends Thread {private static long mini1000000000;private long start,diff;double sum0;double cur1/(double)mini;public pithread(long start,long diff) {this.startstart;this.diffdiff;}Overridepublic void run() {long istart;f…

linux復制文件跳過相同,Linux cp指令,怎么跳過相同的文件

1、使用cp命令的-n參數即可跳過相同的文件 。2、cp命令使用詳解:1)、用法:cp [選項]... [-T] 源文件 目標文件或:cp [選項]... 源文件... 目錄或:cp [選項]... -t 目錄 源文件...將源文件復制至目標文件,或將多個源文件…

eclipse類自動生成注釋

1.創建新類時自動生成注釋 window->preference->java->code styple->code template 當你選擇到這部的時候就會看見右側有一個框顯示出code這個選項,你點開這個選項,點一下他下面的New …

rman恢復

--建表create table sales( product_id number(10), sales_date date, sales_cost number(10,2), status varchar2(20));--插數據insert into sales values (1,sysdate-90,18.23,inactive);commit; --啟用rman做全庫備份 運行D:\autobackup\rman\backup_orcl.bat 生成…

微軟大數據_我對Microsoft的數據科學采訪

微軟大數據Microsoft was one of the software companies that come to hire interns at my university for 2021 summers. This year, it was the first time that Microsoft offered any Data Science Internship for pre-final year undergraduate students.微軟是到2021年夏…

再次檢查打印機名稱 并確保_我們的公司名稱糟透了。 這是確保您沒有的方法。...

再次檢查打印機名稱 并確保by Dawid Cedrych通過戴維德塞德里奇 我們的公司名稱糟透了。 這是確保您沒有的方法。 (Our company name sucked. Here’s how to make sure yours doesn’t.) It is harder than one might think to find a good business name. Paul Graham of Y …

linux中文本查找命令,Linux常用的文本查找命令 find

一、常用的文本查找命令grep、egrep命令grep:文本搜索工具,根據用戶指定的文本模式對目標文件進行逐行搜索,先是能夠被模式匹配到的行。后面跟正則表達式,讓grep工具相當強大。-E之后還支持擴展的正則表達式。# grep [options] …

分布與并行計算—日志挖掘(Java)

日志挖掘——處理數據、計費統計 1、讀取附件中日志的內容,找出自己學號停車場中對應的進出車次數(in/out配對的記錄數,1條in、1條out,視為一個車次,本日志中in/out為一一對應,不存在缺失某條進或出記錄&a…

《人人都該買保險》讀書筆記

內容目錄: 1.你必須知道的保險知識 2.家庭理財的必需品 3.保障型保險產品 4.儲蓄型保險產品 5.投資型保險產品 6.明明白白買保險 現在我所在的公司Manulife是一家金融保險公司,主打業務就是保險,因此我需要熟悉一下保險的基礎知識&#xff0c…

Linux下查看txt文檔

當我們在使用Window操作系統的時候,可能使用最多的文本格式就是txt了,可是當我們將Window平臺下的txt文本文檔復制到Linux平臺下查看時,發現原來的中文所有變成了亂碼。沒錯, 引起這個結果的原因就是兩個平臺下,編輯器…

如何擊敗騰訊_擊敗股市

如何擊敗騰訊個人項目 (Personal Proyects) Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an…

滑塊 組件_組件制作:如何使用鏈接的輸入創建滑塊

滑塊 組件by Robin Sandborg羅賓桑德伯格(Robin Sandborg) 組件制作:如何使用鏈接的輸入創建滑塊 (Component crafting: how to create a slider with a linked input) Here at Stacc, we’re huge fans of React and the render-props pattern. When it came time…

配置靜態IPV6 NAT-PT

一.概述: IPV6 NAT-PT( Network Address Translation - Port Translation)應用與ipv4和ipv6網絡互訪的情況,根據參考鏈接配置時出現一些問題,所以記錄下來。參考鏈接:http://www.cisco.com/en/US/tech/tk648/tk361/technologies_c…