Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript private class fields or methods not working in iOS Chrome or Safari

Do Javascript private fields or methods using # work in iOS?

For example, I have this class:

Class A { 
    #privateFunction() {return ""} 
}

Creating an instance of class A works in Android and desktop. But in iOS, I get the following error: cannot find variable: A

I know the error is related to iOS, because if I made the privateFunction public, it works.

I'm testing the code on a real iPhone device updated to the latest iOS version.

Update: I updated iOS to 14.5.1 but nothing changed.

like image 815
Zakaria M. Jawas Avatar asked Oct 21 '25 00:10

Zakaria M. Jawas


1 Answers

On iOS, even Chrome uses Apple's JavaScriptCore JavaScript engine instead of its usual V8 (and uses the WebKit rendering engine, instead of the usual Blink) because of Apple policies¹ (this is true of all other browsers as well).

JavaScriptCore only just added support for private fields in iOS 14.5, so you'll need to be on a completely up-to-date device (as of this writing). The proposal is no longer "in progress," it's in the specification (as of the last TC39 meeting).


¹ They can't use their own JavaScript engines because that would require allocating executable memory, which Apple only allows Apple apps to do. (V8 did add an interpreter-only mode, but JavaScriptCore is quite a good JavaScript engine and the iOS Chrome folks just use it instead.) They can't use their own rendering engines because Apple doesn't want them to, you have to use their WebView component instead.

like image 62
T.J. Crowder Avatar answered Oct 22 '25 16:10

T.J. Crowder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!